检查当前用户是否是WordPress中的管理员


Check if current user is administrator in wordpress

我正在为 wordpress 开发一个插件,我想找出当前用户是否是管理员,不幸的是我无法使用current_user_can(),因为它给了我错误,所以我使用全局$current_user。但是即使对于管理员用户,我也无法进入if部分。如何解决这个问题?

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

尝试如下操作:

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

在此处阅读有关current_user_can功能的更多信息。

获取用户并检查它是否具有 adminstrator 角色,如下所示:

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

我有用:

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

如果不是多站点设置,则可以使用它来检测管理员。如果是多站点,则只会为超级用户返回 true。

  $user_ID = get_current_user_id();
  if($user_ID && is_super_admin( $user_id )) {
    Do Something
  }
我知道

这是一个老问题,但我想通过解决实际问题使此页面更有用。这里的实际问题是 OP 无法在他的插件中使用current_user_can( 'manage_options' )。使用该函数会引发通常的 PHP undefined function...错误。发生这种情况是因为插件在 WP 核心完成加载之前被初始化。修复非常简单。在适当的时间加载插件是关键。

假设管理插件代码驻留在类MyPlugin内,类初始化应该挂接到init。以下是一种方法

/**
 * 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();
});

这是一种确保 wordpress 核心可用于插件函数的方法。

您可以在此处找到admin_init文档。

附言你应该考虑使用PHP HEREDOC。这是一种非常简单的多行字符串编写方法。您的样式块可以重写

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

我希望它能帮助到某人。

谢谢。

回答

这个问题为时已晚,但我认为如果有人像我一样最终来到这里,无论如何它可能会有用。

我需要解决此问题的快速解决方案 - 检查当前用户是否是管理员。

从WP法典中,我得到了一个简单的解决方案,那就是使用..

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

根据WP-Codex,如果当前登录的用户是网络(超级)管理员,则此函数返回True。 即使禁用了网络模式,但当前用户是管理员,此函数也返回 True

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

更多信息在这里: 如何检查用户是WordPress中的管理员还是编辑

使用此代码,我希望这可以解决您的问题

global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
echo trim($user_role);
$user=wp_get_current_user();
if(in_array("administrator", $user->roles)){
  //user role is admin
}