Matt
Matt

Reputation: 1577

How to check if currently in Wordpress Admin?

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

Upvotes: 44

Views: 61164

Answers (5)

theRunner
theRunner

Reputation: 179

See is_admin_request() for a working solution.

Upvotes: 1

Juniper Jones
Juniper Jones

Reputation: 799

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
}

Upvotes: 23

Gaurab paul
Gaurab paul

Reputation: 9

<?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
}
?>

Upvotes: -4

psycho brm
psycho brm

Reputation: 7664

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.

Upvotes: 28

Matt
Matt

Reputation: 1577

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

Upvotes: 73

Related Questions