检测当前的用户是否有特定的权限,使用之前要确保全局的 $current_user 已经被设置了。
<?php current_user_can( $capability, $args ); ?>
$capability
(string) (required) 权限或者角色名称
Default: None
$args
(mixed) (optional) 额外需要的参数,比如 Post ID,一些权限检测(比如 'edit_post' 或者 'delete_page')需要提供这个参数。
Default: None
(bool)
如果当前用户有该权限返回 true,否则返回 false。
Since: 2.0.0
wp-includes/capabilities.php
current_user_can( $capability, $args );
if( current_user_can('administrator') ) { echo '這個文字只有管理員才能看的到';}
或者
if( current_user_can('level_10') ) { echo '這個文字只有管理員才能看的到';}
level 数字的判断方式,。官方已经建议不要再使用了,应尽量避免。
1 判断用户是否为管理员(administrator)
if ( current_user_can ( 'manage_options' ) ) { echo 'The current user is a administrator' ;}
2 判断用户是否为编辑(Editor)
if ( current_user_can ( 'publish_pages' ) && ! current_user_can ( 'manage_options' ) ) { echo 'The current user is an editor' ;}
3 判断用户是否为作者(Author)
if ( current_user_can ( 'publish_posts' ) && ! current_user_can ( 'publish_pages' ) ) { echo 'The current user is an author' ;}
4 判断用户是否为投稿者(Contributor)
if ( current_user_can ( 'edit_posts' ) && ! current_user_can ( 'publish_posts' ) ) { echo 'The current user is a contributor' ; }
5 判断用户是否为订阅者(Subscriber)
if ( current_user_can ( 'read' ) && ! current_user_can ( 'edit_posts' ) ) { echo 'The current user is a subscriber' ; }