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

參考鏈接


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM