wordpress current_user_can 判断角色

current_user_can

使用说明:

current_user_can( $capability, $args );

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

角色判断

if( current_user_can('administrator') ) {

    echo '這個文字只有管理員才能看的到';

}

或者

if( current_user_can('level_10') ) {

    echo '這個文字只有管理員才能看的到';

}

  • 补充说明:

level 数字的判断方式,。官方已经建议不要再使用了,应尽量避免。

  • level_10 ~ level_8:只有 administrator 等级有权限

  • level_7 ~ level_3:只有 administrator、editor 等级有权限

  • level_2:只有 administrator、editor、author 等级有权限

  • level_1:只有 administrator、editor、author、contributor 等级有权限

  • level_0:只有 administrator、editor、author、contributor、subscriber 等级有权限

  • 管理员:administrator

  • 编辑: editor

  • 作者: autor

  • 投稿者: contributor

  • 订阅者:subscriber

使用举例:

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' ; 
}

参考链接

posted @ 2019-10-05 02:59  绝技小嗨皮  阅读(1287)  评论(0编辑  收藏  举报
Title