wordpress current_user_can 判断角色
本文最后更新于1401天前,其中的信息可能已经有所发展或是发生改变。

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 数字的判断方式,。官方已经建议不要再使用了,应尽量避免。

wordpress current_user_can 判断角色

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

参考链接

点击数:107

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇