43

I am creating my first plugin and have a single function that controls the output. This function has different output based on whether or not it is being viewed from within the WordPress admin vs. the frontend. Is there any way to easily test whether or not my function is being triggered from within admin vs the frontend?

I've tried conditionally checking the query string against the name of my plugin "page" name but it seems to fail on some servers/installs.

Thanks

Matt
  • 1,539
  • 1
  • 11
  • 16

5 Answers5

71

Duh, this was too obvious. For some reason I was thinking this had to do with an admin user. if(is_admin()) { ...output my admin stuff....}

http://codex.wordpress.org/Function_Reference/is_admin

Matt
  • 1,539
  • 1
  • 11
  • 16
  • I needed the same. I fell for the same. – Frank N Feb 27 '12 at 06:29
  • 4
    Warning warning Will Robinson: this code checks to see if you are in the admin area, NOT whether you are logged in as an admin!! – Brian C Mar 21 '13 at 08:45
  • @BrianC - Right. I wasn't trying to determine if the user is logged in. My original question was about trying to determine if the current page was being rendered in the frontend or admin. So this is the correct function if you want to do something only while in the admin area. – Matt Mar 21 '13 at 13:21
  • 1
    is_admin() also returns true for front-end Ajax requests ? – zdenekca Mar 05 '15 at 15:07
25

If you want to know whether current user IS ADMIN, then you should use this:

   $is_admin = current_user_can( 'manage_options' );

I got misguided by the above answer, so a little note to keep others from making the same mistake.

psycho brm
  • 7,494
  • 1
  • 43
  • 42
  • Yes, I think this seems to be the proper one that even the Wordpress Support puts it down. – Salocin.TEN Apr 15 '13 at 09:11
  • 2
    If your not working with the current user you can use user_can(1,'manage_options') with "1" being the user ID. Again though just like "current_user_can()" this has nothing to do with checking if user is viewing and administration page. – Ryan Bayne Sep 25 '13 at 15:21
23

Note that is_admin() only works in the backend. For any part of the plugin that shows on the public website you need to use current_user_can().

if ( current_user_can( 'administrator' ) ) {
  // your code goes here
}
Volker E.
  • 5,911
  • 11
  • 47
  • 64
Juniper Jones
  • 779
  • 6
  • 10
  • Can you please add a reference for current_user_can('administrator') ? 'administrator' is not listed on https://codex.wordpress.org/Function_Reference/current_user_can – tim-we Apr 19 '15 at 01:30
  • 1
    WARNING: if an `admin-ajax.php` request is coming through (from the frontend), `is_admin()` will still return TRUE – sMyles Oct 30 '20 at 19:53
1

See is_admin_request() for a working solution.

theRunner
  • 179
  • 1
  • 8
-4
<?php 
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID

//usually admin user id is 1 if its not working check admin user id from wp_users table
if($user_id == 1) {
   //write your stuff
}
?>
  • 2
    I think current_user_can() is probably a more effective method, probably not wise to be checking WordPress global vars. – Brian C Mar 21 '13 at 10:16
  • 2
    Correct Brian. We should never assume a user ID is a certain user never-mind an administrator. – Ryan Bayne Sep 25 '13 at 15:12