Check if current user is administrator in wordpres

2019-02-02 13:44发布

问题:

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'  );
}

回答1:

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.



回答2:

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';
}


回答3:

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
  }


回答4:

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.



回答5:

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.



回答6:

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);


回答7:

Tested and it works fine

if(current_user_can('administrator') ) {
   echo 'ok' ;
}else{
   echo 'out' ;
}


回答8:

$user=wp_get_current_user();
if(in_array("administrator", $user->roles)){
  //user role is admin
}