php 检查当前用户是否是 wordpress 中的管理员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19802492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if current user is administrator in wordpress
提问by NEO
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?
我正在为 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' );
}
回答by Gogol
回答by Tosh
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';
}
回答by codewaggle
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.
如果它不是多站点设置,您可以使用它来检测管理员。如果是多站点,这只会为超级管理员返回 true。
$user_ID = get_current_user_id();
if($user_ID && is_super_admin( $user_id )) {
Do Something
}
回答by Ejaz
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.
我知道这是一个老问题,但我想通过解决实际问题使这个页面更有用。这里的实际问题是 OP 无法current_user_can( 'manage_options' )
在他的插件中使用。使用该函数会引发常见的undefined function...
PHP 错误。发生这种情况是因为插件在 WP 核心完成加载之前被初始化。修复非常简单。在适当的时候加载插件是关键。
Assuming the admin plugin code resides inside a class MyPlugin
, the class initialization should be hooked to init
. Following is oneway of doing it.
假设管理插件代码驻留在一个类中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();
});
This is a way of making sure that wordpress core is available to the plugin function.
这是确保插件功能可以使用 wordpress 核心的一种方法。
You can find admin_init
documentation here.
您可以在此处找到admin_init
文档。
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
PS 您应该考虑使用PHP HEREDOC。这是编写多行字符串的一种非常简单的方法。您的样式块可以重写如下
$hide_post_options = <<<HTML
<style type="text/css">
.jaxtag { display: none; }
#category-adder { display: none; }
</style>
HTML;
I hope it helps somebody.
我希望它可以帮助某人。
Thanks.
谢谢。
回答by miyaneha
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 codexI got a simple solution which is to use..
从WP codex我得到了一个简单的解决方案,即使用..
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.
根据 WP-Codex,如果当前登录的用户是网络(超级)管理员,则此函数返回 True。即使网络模式被禁用但当前用户是 admin ,此函数也会返回 True。
回答by Yatendra
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);
回答by File_Submit
<?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
?>
More info here: How To Check If User Is Administrator Or Editor In WordPress
回答by Brad
$user=wp_get_current_user();
if(in_array("administrator", $user->roles)){
//user role is admin
}