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

NEO*_*NEO 24 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'  );
}
Run Code Online (Sandbox Code Playgroud)

Gog*_*gol 49

尝试以下内容:

if ( current_user_can( 'manage_options' ) ) {
    /* A user with admin privileges */
} else {
    /* A user without admin privileges */
}
Run Code Online (Sandbox Code Playgroud)

阅读更多关于此current_user_can功能的信息.


Tos*_*osh 22

获取用户并检查其是否具有角色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';
}
Run Code Online (Sandbox Code Playgroud)


cod*_*gle 10

这对我有用:

  global $current_user;

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

如果它不是多站点设置,您可以使用它来检测管理员.如果它是多站点,则仅对超级管理员返回true.

  $user_ID = get_current_user_id();
  if($user_ID && is_super_admin( $user_id )) {
    Do Something
  }
Run Code Online (Sandbox Code Playgroud)


Fil*_*mit 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

?>
Run Code Online (Sandbox Code Playgroud)

更多信息请参见:如何检查用户是否是 WordPress 中的管理员或编辑者


Eja*_*jaz 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();
});
Run Code Online (Sandbox Code Playgroud)

这是一种确保wordpress核心可用于插件功能的方法.

你可以在这里找到admin_init文档.

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

$hide_post_options = <<<HTML
<style type="text/css"> 
    .jaxtag { display: none; } 
    #category-adder { display: none; }
</style>
HTML;
Run Code Online (Sandbox Code Playgroud)

我希望它对某人有所帮助.

谢谢.


小智 5

对于这个问题的答案为时已晚,但是我认为如果有人像我一样最终来到这里,这可能还是有用的。

我需要快速解决此问题的方法-检查当前用户是否为admin。

WP Codex,我得到了一个简单的解决方案。

if(is_super_admin($user_id)) {
  // do stuff for the admin user...
}
Run Code Online (Sandbox Code Playgroud)

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


Yat*_*dra 3

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

global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
echo trim($user_role);
Run Code Online (Sandbox Code Playgroud)