检查当前用户是否是 wordpress 中的管理员 [英] Check if current user is administrator in wordpress

查看:37
本文介绍了检查当前用户是否是 wordpress 中的管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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 函数的更多信息.

Read more about the current_user_can function here.

这篇关于检查当前用户是否是 wordpress 中的管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆