【问题标题】:Check if current user is administrator in wordpress检查当前用户是否是wordpress中的管理员
【发布时间】:2013-11-17 02:58:37
【问题描述】:

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

【问题讨论】:

  • 错误是什么,您要检查什么?您使用的是当前版本的 wp 吗?例如,current_user_can('manage_sites') 适用于超级管理员。也许你传递了一个无效的权限
  • 出现致命错误:当我使用 current_user_can 时,在第 1286 行的 /Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-includes/capabilities.php 中调用未定义函数 wp_get_current_user()。
  • 你读过这个吗:wordpress.org/support/topic/…
  • 你搜索过Check if current user is administrator in wordpress 吗?
  • 如果您在管理面板中,is_admin() 很有用。但是,如果您只想检查角色,则不是-尽管现在我回顾一下这个问题,他似乎正在添加管理员 css,所以您可能是对的

标签: php wordpress administrator


【解决方案1】:

使用这段代码,希望能解决你的问题

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

【讨论】:

    【解决方案2】:

    尝试以下方法:

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

    详细了解current_user_can 函数here

    【讨论】:

      【解决方案3】:

      这对我有用:

        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
        }
      

      【讨论】:

      • if(in_array('administrator', $current_user-&gt;roles)) { 是做同样事情的更简洁的方式
      • WordPress 真的需要一个像 is_this_user_admin() 这样的函数——你会认为 is_admin() 会这样做,但事实并非如此!也许这是因为他们希望我们改用角色,但为了清楚起见,确实应该将 is_admin() 重命名为 is_dashboard_active()。
      【解决方案4】:

      获取用户并检查其是否具有管理员角色,如下所示:

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

      【讨论】:

      • 我认为这是最简单的。
      【解决方案5】:

      我知道这是一个老问题,但我想通过解决实际问题使此页面更有用。这里的实际问题是 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

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

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

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

      谢谢。

      【讨论】:

        【解决方案6】:

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

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

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

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

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

        【讨论】:

          【解决方案7】:
          $user=wp_get_current_user();
          if(in_array("administrator", $user->roles)){
            //user role is admin
          }
          

          【讨论】:

          • 欢迎来到 SO。显然,这个答案已经付出了很多努力,如果它只是代码,可能很难掌握。通常用几句话来评论解决方案。请编辑您的答案并添加一些解释。
          【解决方案8】:
          <?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

          【讨论】:

            猜你喜欢
            • 2011-04-05
            • 1970-01-01
            • 1970-01-01
            • 2018-02-02
            • 1970-01-01
            • 2015-10-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多