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

时间:2013-11-06 01:37:02

标签: php wordpress administrator

我正在为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'  );
}

9 个答案:

答案 0 :(得分:47)

尝试以下内容:

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

详细了解current_user_can函数here

答案 1 :(得分:17)

获取用户并检查其是否具有角色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';
}

答案 2 :(得分:9)

这对我有用:

  global $current_user;

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

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

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

答案 3 :(得分:6)

我知道这是一个老问题,但我想通过解决实际问题使这个页面更有用。这里的实际问题是OP还没有能够在他的插件中使用current_user_can( 'manage_options' )。使用该函数会引发通常的undefined function... PHP错误。发生这种情况是因为插件在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文档here

P.S。您应该考虑使用PHP HEREDOC。这是一种编写多行字符串的简单方法。您的样式块可以重写如下

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

我希望它有所帮助。

感谢。

答案 4 :(得分:4)

对于这个问题的答案来说太迟了,但我认为如果有人像我一样在这里结束可能会有用。

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

WP codex我得到了一个简单的解决方案,即使用..

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

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

答案 5 :(得分:2)

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

global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
echo trim($user_role);

答案 6 :(得分:1)

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

?>

此处有更多信息:How To Check If User Is Administrator Or Editor In WordPress

答案 7 :(得分:0)

经过测试,效果很好

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

答案 8 :(得分:0)

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