WordPress判断用户特定内容只对管理员可见
- 发表于
- WordPress
WordPress特定内容只对管理员可见
之前判断登录用户的角色用的是current_user_can()方法,比如判断当前用户是否是作者用
current_user_can('author')
,但对于WordPress 5不再可行。current_user_can的用法变了,正确的用法是传递
$capability
。除此之外,还有哪些更高效的方法来判断WordPress用户角色呢?
今天体验盒子在baidu和Google谷歌镜像上搜了一圈“WordPress判断用户角色和权限及管理员,WordPress特定内容只对管理员可见”,发现很多结果都过于老旧,于是重新总结了一下,本文内容适用于最新的WordPress5。
一、用current_user_can判断
当前用户是否具有特定权限与功能。
| 1 | <?php current_user_can( $capability , $object_id ); ?> |
- $capability(string) (必须) [角色或功能].默认: 无
- $object_id(int) (可选的) .默认: 无
不要将角色名称传递给current_user_can(),因为这不能保证其正常工作。传递用户角色名称(如author、contributor)作为参数不能100%保证返回正确的结果,正确的用法是传递$capability,用权限值比角色名称更靠谱。
所以,要根据不同角色拥有的权限来判断用户角色,用户权限可以在Roles and Capabilities中找到。
判断示例
判断用户是否为管理员(Administrator)
| 1 2 3 | if( current_user_can( 'manage_options' ) ) { echo 'The current user is a administrator'; } |
判断用户是否为编辑(Editor)
| 1 2 3 | if( current_user_can( 'publish_pages' ) && !current_user_can( 'manage_options' ) ) { echo 'The current user is an editor'; } |
判断用户是否为作者(Author)
| 1 2 3 | if( current_user_can( 'publish_posts' ) && !current_user_can( 'publish_pages' ) ) { echo 'The current user is an author'; } |
判断用户是否为投稿者(Contributor)
| 1 2 3 | if( current_user_can( 'edit_posts' ) && !current_user_can( 'publish_posts' ) ) { echo 'The current user is a contributor'; } |
判断用户是否为订阅者(Subscriber)
| 1 2 3 | if( current_user_can( 'read' ) && !current_user_can( 'edit_posts' ) ) { echo 'The current user is a subscriber'; } |
二、用$current_user判断
$current_user是WordPress的一个全局变量,当用户登录后,这个里面就会有用户的角色和权限信息。当WordPress的init action执行后,就可以安全的使用$current_user全局变量了。
在模板文件中判断登录用户是否为作者(Author)
| 1 2 3 4 | global $current_user; if( $current_user->roles[0] == 'author' ) { echo 'The current user is an author'; } |
在functions.php中判断用户是否为作者(Author)
| 1 2 3 4 5 6 7 | add_action( 'init', 'check_user_role' ); function check_user_role() { global $current_user; if( $current_user->roles[0] == 'author' ) { echo 'The current user is an author'; } } |
检查用户角色之前,还可以先检查一下用户是否登录
| 1 2 3 4 5 | <?php if( is_user_logged_in() ) { //用户已登录,检查用户角色 } ?> |
三、使用WordPress Levels判断用户角色和权限及管理员
扩展一下。我们用第一个方法的
current_user_can
来更灵活的判断用户角色、权限、管理员,下图展示了WordPress各个用户组所对应的级别,那么我们需要来判断是否为管理员,也就是
Administrator(level_10)
:

用户级别功能表
| WordPress User Level | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 空白单元格意味着该功能甚至不适用于特定的用户级别。 “-”表示用户级别具有功能,但仅部分功能。 “ +”表示用户级别具有该功能,但只能影响其自己的对象(例如,帖子)或较低用户级别的对象。 “ x”表示用户具有该菜单项可用的全部功能。 | |||||||||||
| User Level: | |||||||||||
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
| Dashboard | |||||||||||
| ----- | x | x | x | x | x | x | x | x | x | x | x |
| Write | |||||||||||
| -----Write Post | - | x | x | x | x | x | x | x | x | x | |
| -----Write Page | x | x | x | x | x | x | |||||
| Manage | |||||||||||
| -----Posts | - | + | + | + | + | + | + | + | + | x | |
| -----Pages | + | + | + | + | + | x | |||||
| -----Categories | - | - | - | x | x | x | x | x | x | x | |
| -----Comments | x | x | x | x | x | x | x | x | x | x | |
| -----Awaiting Moderation | - | - | - | x | x | x | x | x | x | x | |
| Links | |||||||||||
| -----Manage Links | + | + | + | + | + | x | |||||
| -----Add Links | x | x | x | x | x | x | |||||
| -----Link Categories | x | x | x | x | x | x | |||||
| -----Import Links | x | x | x | x | x | x | |||||
| Presentation | |||||||||||
| -----Themes | x | x | x | ||||||||
| -----Theme Editor | x | x | x | ||||||||
| Plugins | |||||||||||
| -----Plugins | x | x | x | ||||||||
| -----Plugin Editor | x | x | x | ||||||||
| Users | |||||||||||
| -----Your Profile | x | x | x | x | x | x | x | x | x | x | x |
| -----Authors and Users | - | + | + | + | + | x | |||||
| Options | |||||||||||
| -----General | x | x | x | x | x | ||||||
| -----Writing | x | x | x | x | x | ||||||
| -----Reading | x | x | x | x | x | ||||||
| -----Discussion | x | x | x | x | x | ||||||
| -----Permalinks | x | x | x | x | x | ||||||
| -----Miscellaneous | x | x | x | x | x | ||||||
| Upload | |||||||||||
| -----(only if enabled) | x | x | x | x | x | x | x | x | x | x | |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
用
level_10
判断是否为wordpress管理员代码如下
| 1 2 3 | if(current_user_can('level_10')){ //加入符合管理员后需要添加的内容 } |
这样想判断其他权限用户也就是换个级别就可以了。
四、更简单高效的方法
还有一种更直接的方法,例如判断当前用户是否为管理员
| 1 2 3 4 | global $current_user; if(in_array( 'administrator', $current_user->roles )){ echo 'administrator'; } |
也可以这样
| 1 2 3 | <?php global $user_ID; if( $user_ID && current_user_can('level_10') ) : ?> administrator <?php endif; ?> |
不太严谨的高效写法,需要修改administrator为你的管理员用户名
| 1 2 3 4 5 | <?php if ( $GLOBALS['user_login'] != 'administrator' ) { echo 'administrator'; } ?> |
原文连接
的情况下转载,若非则不得使用我方内容。