wordpress角色能力

说到角色能力,我们先来说一个函数
current_user_can
看一下使用

current_user_can( $capability, $args );

capability参数是必须的,是指给予什么样的能力,或者说给他可以做什么;args 参数是非必要的,是额外给予的参数,例如:current_user_can('edit_post', 121),意指给他可以编辑编号为121的文章。

角色判断

if( current_user_can('administrator') ) {
    echo '文字只有管理员才能看的到';
}

使用举例:

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' ; 
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容