Check if current user is administrator in wordpress
Asked Answered
N

8

36

I am developing a plugin for wordpress, I want to find if current user is administrator or not, unfortunately I could not use the current_user_can() as it gives me error, so am using the global $current_user. But I could not get inside the if part even for admin user.. How to fix this?

global $current_user;
if ($current_user->role[0]=='administrator'){
function hide_post_page_options() {
//global $post;
// Set the display css property to none for add category and add tag functions
$hide_post_options = "<style type=\"text/css\"> .jaxtag { display: none; } #category-adder { display: none; } </style>";
print($hide_post_options);
}
add_action( 'admin_head', 'hide_post_page_options'  );
}
Nocturne answered 6/11, 2013 at 1:37 Comment(12)
What is the error and what are you checking against? Are you using the current version of wp? current_user_can('manage_sites') would work for super admins, for example. Maybe you're passing an invalid permissionPleistocene
Am getting Fatal error: Call to undefined function wp_get_current_user() in /Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-includes/capabilities.php on line 1286 when i use current_user_can.Nocturne
Have you read this: wordpress.org/support/topic/…Pleistocene
Have you googled Check if current user is administrator in wordpress ?Parker
I added the requireonce in capabilities.php and it works.. But, I need to pack this plugin that should work without that fix, is there any way for this?Nocturne
I think the article mentioned something about the require being necessary because of the load order. You may be able to make use of wp's hook system to ensure your plugin gets loaded after the core requirements are met. Is there a reason why you must not use this hack for your plugin? So much of WP is a hack anyhow, who is going to care?Pleistocene
Um, running the Google query mentioned above gives me an is_admin() function that seems to be exactly what you need?Parker
is_admin() is useful if you are in the admin panel. But not if you just want to check roles - although now that i look back on the question, he appears to be adding admin css, so you may be rightPleistocene
@KaiQing I want to ensure its compatible without making any changes in wp files. Isn't there any alternative at all without using requireonce modification?Nocturne
if (!is_admin()) is not working too.. !current_user_can('administrator') is only working with modification in capabilities.phpNocturne
Oh right right. the dependency thing. I overlooked it being a core modification. Interesting thing is I just implemented this for a plugin I'm working on as we speak and I get no error. Latest version of wordpress. That's why I commented.Pleistocene
I'm using the current version of wordpress. I could not get it working without the modification..is it a bug which will be fixed in next wordpress? or am I wrong in the implementation? coz this seems to be the only way I could get it work..Nocturne
K
60

Try something like the following:

if ( current_user_can( 'manage_options' ) ) {
    /* A user with admin privileges */
} else {
    /* A user without admin privileges */
}

Read more about the current_user_can function here.

Kast answered 6/11, 2013 at 6:44 Comment(0)
M
34

Get the user and check if it has the role adminstrator, like so:

function is_site_admin(){
    return in_array('administrator',  wp_get_current_user()->roles);
}

if (is_site_admin()) {
  echo 'Woot Woot';
} else {
  echo 'So sad, I have no rights';
}
Morning answered 24/1, 2014 at 11:39 Comment(1)
I think this is the easiest.Counterblow
C
10

This works for me:

  global $current_user;

  if( !empty($current_user->roles) ){
    foreach ($current_user->roles as $key => $value) {
      if( $value == 'administrator' ){
        Do Something
      }
    }
  }

If it's not a multi-site set-up, you can use this to detect an administrator. If it's multi-site, this will only return true for a super admin.

  $user_ID = get_current_user_id();
  if($user_ID && is_super_admin( $user_id )) {
    Do Something
  }
Colyer answered 12/12, 2013 at 10:31 Comment(2)
if(in_array('administrator', $current_user->roles)) { is a more succinct way of doing the same thingMimimimic
WordPress really needs a function like is_this_user_admin() - you'd think is_admin() would do that, but it doesn't! Perhaps this is because they want us to use roles instead, but is_admin() should really be renamed to is_dashboard_active() for clarity's sake.Deserved
S
8

I know it is an old question but I would like to make this page more useful by addressing the actual issue. The actual issue here is that OP hasn't been able to use current_user_can( 'manage_options' ) in his plugin. Using the function raises the usual undefined function... PHP error. This happens because the plugin gets initialized before WP core completes loading. Fix is very simple. Loading the plugin at appropriate time is the key.

Assuming the admin plugin code resides inside a class MyPlugin, the class initialization should be hooked to init. Following is one way of doing it.

/**
 * Plugin Class
 */
class MyPlugin{
    public function __construct(){
        /* all hooks and initialization stuff here */

        /* only hook if user has appropriate permissions */
        if(current_user_can('manage_options')){
            add_action( 'admin_head', array($this, 'hide_post_page_options'));
        }
    }

    function hide_post_page_options() {
        // Set the display css property to none for add category and add tag functions
        $hide_post_options = "
        <style type=\"text/css\"> 
            .jaxtag { display: none; } 
            #category-adder { display: none; } 
        </style>";

        print($hide_post_options);
    }
}

add_action('admin_init', function(){
    $myplugin = new MyPlugin();
});

This is a way of making sure that wordpress core is available to the plugin function.

You can find admin_init documentation here.

P.S. You should look into using PHP HEREDOC. It is a very simple way of writing multi-line strings. Your style block can be re-written as follows

$hide_post_options = <<<HTML
<style type="text/css"> 
    .jaxtag { display: none; } 
    #category-adder { display: none; }
</style>
HTML;

I hope it helps somebody.

Thanks.

Selection answered 13/2, 2016 at 4:50 Comment(0)
G
7
<?php

if( current_user_can( 'administrator' ) ){} // only if administrator
if( current_user_can( 'editor' ) ){} // only if editor
if( current_user_can( 'author' ) ){} // only if author
if( current_user_can( 'contributor' ) ){} // only if contributor
if( current_user_can( 'subscriber' ) ){} // only if subscriber

?>

More info here: How To Check If User Is Administrator Or Editor In WordPress

Germanophile answered 28/4, 2020 at 17:46 Comment(0)
O
6

Too late for an answer for this question, but I think it might be useful anyway if someone ends up here like me.

I needed a quick solution to this problem - check if the current user is admin or not.

From the WP codex I got a simple solution which is to use..

if(is_super_admin($user_id)) {
  // do stuff for the admin user...
}

According to WP-Codex this function returns True if the currently logged in user is network (super) admin. This function returns True even if the network mode is disabled but the current user is admin.

Ocrea answered 17/10, 2016 at 3:42 Comment(0)
D
3

use this code, I hope this solve your problem

global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
echo trim($user_role);
Dori answered 6/11, 2013 at 6:35 Comment(0)
O
0
$user=wp_get_current_user();
if(in_array("administrator", $user->roles)){
  //user role is admin
}
Omor answered 14/11, 2017 at 12:8 Comment(2)
Welcome to SO. As much effort obviously has gone into this answer, it might be difficult to get a grasp at if it's just code. It's usual to comment the solution with a few sentences. Please edit your answer and add some explanation.Chobot
Looks like a fake account....Arela

© 2022 - 2024 — McMap. All rights reserved.