How to check if currently in Wordpress Admin?
Asked Answered
M

5

44

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

Matthus answered 8/11, 2010 at 21:32 Comment(0)
M
73

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

Matthus answered 8/11, 2010 at 21:57 Comment(4)
I needed the same. I fell for the same.Goingson
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!!Gertrudgertruda
@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.Matthus
is_admin() also returns true for front-end Ajax requests ?Peccary
W
27

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.

Washday answered 7/1, 2013 at 16:46 Comment(2)
Yes, I think this seems to be the proper one that even the Wordpress Support puts it down.Vortex
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.Rolling
Y
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
}
Yellowlegs answered 23/10, 2013 at 13:57 Comment(2)
Can you please add a reference for current_user_can('administrator') ? 'administrator' is not listed on codex.wordpress.org/Function_Reference/current_user_canSamite
WARNING: if an admin-ajax.php request is coming through (from the frontend), is_admin() will still return TRUEAgain
O
1

See is_admin_request() for a working solution.

Ohara answered 1/12, 2018 at 14:3 Comment(0)
H
-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
}
?>
Hermon answered 8/1, 2013 at 12:51 Comment(2)
I think current_user_can() is probably a more effective method, probably not wise to be checking WordPress global vars.Gertrudgertruda
Correct Brian. We should never assume a user ID is a certain user never-mind an administrator.Rolling

© 2022 - 2024 — McMap. All rights reserved.