检查当前用户的角色

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检查当前用户的角色相关的知识,希望对你有一定的参考价值。

Checks if a particular user has a role.
  1. /**
  2. * Checks if a particular user has a role.
  3. * Returns true if a match was found.
  4. *
  5. * http://codex.wordpress.org/Roles_and_Capabilities#Subscriber
  6. * http://docs.appthemes.com/tutorials/wordpress-check-user-role-function/
  7. * http://wordpress.org/support/topic/how-to-get-the-current-logged-in-users-role
  8. *
  9.  
  10. * @param string $role Role name.
  11. * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
  12. * @return bool
  13. */
  14. function appthemes_check_user_role( $role, $user_id = null ) {
  15.  
  16. if ( is_numeric( $user_id ) )
  17. $user = get_userdata( $user_id );
  18. else
  19. $user = wp_get_current_user();
  20.  
  21. if ( empty( $user ) )
  22. return false;
  23.  
  24. return in_array( $role, (array) $user->roles );
  25. }
  26.  
  27.  
  28. // example use for the current user
  29. if ( appthemes_check_user_role( 'subscriber' ) ){
  30. show_admin_bar(false);
  31. } else {
  32. _e( "Sorry man, no luck.", 'appthemes' );
  33. }

以上是关于检查当前用户的角色的主要内容,如果未能解决你的问题,请参考以下文章

检查当前用户的角色

用户角色 - 添加角色功能 php 片段 - Wordpress

Wordpress - 通过检查用户状态从 bbPress 外部更改论坛角色

如何检查用户是不是在 Meteor 中具有特定角色

如何修复“代码应明确检查权限是不是可用”错误

检查用户是不是有角色 Discord.net