如何检查用户是否具有特定角色?
6 个回答
- 投票数
-
- 2010-12-08
如果仅当前用户需要此
current_user_can()
既接受角色也接受能力更新:不再保证将角色名称传递给
current_user_can()
(请参见#22624 ).相反,您可能希望检查用户角色:$user = wp_get_current_user(); if ( in_array( 'author', (array) $user->roles ) ) { //The user has the "author" role }
If you only need this for current user
current_user_can()
accepts both roles and capabilities.UPDATE: Passing a role name to
current_user_can()
is no longer guaranteed to work correctly (see #22624). Instead, you may wish to check user role:$user = wp_get_current_user(); if ( in_array( 'author', (array) $user->roles ) ) { //The user has the "author" role }
-
我知道这篇帖子已经很久以前了,但是如果有人碰巧来到这里...请再次查看current_user_can()的文档->"不要将角色名称传递给current_user_can(),因为这不能保证正常工作(请参阅#22624).相反,您可能希望尝试由AppThemes组合使用的check user role功能."(http://codex.wordpress.org/Function_Reference/current_user_can)I know this post is answered a long time ago but if someone happens to get here... look at the documentation once more for current_user_can() -> "Do not pass a role name to current_user_can(), as this is not guaranteed to work correctly (see #22624). Instead, you may wish to try the check user role function put together by AppThemes." (http://codex.wordpress.org/Function_Reference/current_user_can)
- 10
- 2014-01-28
- bestprogrammerintheworld
-
^if语句中缺少括号^ There is a bracket missing in the if statement
- 1
- 2015-06-04
- Aajahid
-
@Aajahid编辑:)@Aajahid edited :)
- 1
- 2015-06-04
- Rarst
-
如果不使用多站点,我仍然更喜欢current_user_can('editor')的简单性.If not using multisite, I still prefer the simplicity of `current_user_can('editor')`
- 1
- 2020-01-25
- Jules
-
- 2012-06-11
我一直在寻找一种使用用户ID来获取用户角色的方法.这是我想出的:
function get_user_roles_by_user_id( $user_id ) { $user = get_userdata( $user_id ); return empty( $user ) ? array() : $user->roles; }
然后,可以像这样实现
is_user_in_role()
函数:function is_user_in_role( $user_id, $role ) { return in_array( $role, get_user_roles_by_user_id( $user_id ) ); }
I was looking for a way to get a user's role using the user's id. Here is what I came up with:
function get_user_roles_by_user_id( $user_id ) { $user = get_userdata( $user_id ); return empty( $user ) ? array() : $user->roles; }
Then, an
is_user_in_role()
function could be implemented like so:function is_user_in_role( $user_id, $role ) { return in_array( $role, get_user_roles_by_user_id( $user_id ) ); }
-
我可以很好地工作,以将第一个角色分配给用户.works fine for me to get the first role assigned to a user.
- 1
- 2012-10-10
- Q Studio
-
那么分配给用户的所有角色呢?What about all the roles assigned to the user?
- 0
- 2017-04-10
- Sahu V Kumar
-
@Vishal Kumar,这将检查分配给用户的所有角色.@Vishal Kumar this will check against all roles assigned to the user.
- 1
- 2017-04-10
- Stephen M. Harris
-
此功能不存在,不确定它是旧的还是什么,但是您应该使用上面的答案或我在下面发布的答案This function does not exist, not sure if it was just old or what, but you should use the answer above or the one I posted below
- 0
- 2017-11-16
- sMyles
-
- 2017-11-16
您也可以只创建一个新的用户对象:
$user = new WP_User( $user_id ); if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'Some_role', $user->roles ) ) { return true; }
不确定删除了哪个版本的
get_user_roles_by_user_id
,但该功能不再可用.You can also just create a new user object:
$user = new WP_User( $user_id ); if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'Some_role', $user->roles ) ) { return true; }
Not sure what version
get_user_roles_by_user_id
was removed in, but it's no longer an available function.-
当我必须调用WP_User类的其他方法时,这很方便.This is handy when I have to call other methods of the WP_User class.
- 0
- 2019-09-25
- Justin Waulters
-
- 2017-11-28
这是一个接受用户和角色以提高灵活性的功能:
函数my_has_role($ user,$ role){ $ roles=$ user-> roles; 返回in_array($ role,(array)$ user-> roles); } if(my_has_role($ user,'some_role')){ //做东西 }
Here is a function that accepts a user and role for greater flexibility:
function my_has_role($user, $role) { $roles = $user->roles; return in_array($role, (array) $user->roles); } if(my_has_role($user, 'some_role')) { //do stuff }
-
- 2019-10-02
用户对象
获取当前用户ID.$user->roles
上的调用角色不会返回所有角色.以下是查找用户是否具有角色或能力的正确方法.(这在wp 2.0.0及更高版本中有效.)以下函数与用户ID配合使用,您可以通过$current_user_id = get_current_user_id();
/** * Returns true if a user_id has a given role or capability * * @param int $user_id * @param string $role_or_cap Role or Capability * * @return boolean */ function my_has_role($user_id, $role_or_cap) { $u = new \WP_User( $user_id ); //$u->roles Wrong way to do it as in the accepted answer. $roles_and_caps = $u->get_role_caps(); //Correct way to do it as wp do multiple checks to fetch all roles if( isset ( $roles_and_caps[$role_or_cap] ) and $roles_and_caps[$role_or_cap] === true ) { return true; } }
Calling roles on User Object
$user->roles
do not return all the roles. The correct way to find if the user has a role or capability is following. (This works in wp version 2.0.0 and greater.) The following function works with user id you can get the current user id by$current_user_id = get_current_user_id();
/** * Returns true if a user_id has a given role or capability * * @param int $user_id * @param string $role_or_cap Role or Capability * * @return boolean */ function my_has_role($user_id, $role_or_cap) { $u = new \WP_User( $user_id ); //$u->roles Wrong way to do it as in the accepted answer. $roles_and_caps = $u->get_role_caps(); //Correct way to do it as wp do multiple checks to fetch all roles if( isset ( $roles_and_caps[$role_or_cap] ) and $roles_and_caps[$role_or_cap] === true ) { return true; } }
-
- 2020-07-27
这是旧文章,但这是一个通用功能,适用于所有WordPress版本.
if(!function_exists('is_user')): function is_user ($role=NULL, $user_id=NULL) { if(empty($user_id)){ $user = wp_get_current_user(); } else { if(is_numeric($user_id) && $user_id == (int)$user_id) { $user = get_user_by('id', (int)$user_id); } else if(is_string($user_id) && $email = sanitize_email($user_id)) { $user = get_user_by('email', $email); } else { return false; } } if(!$user) return false; return in_array( $role, (array)$user->roles, true ) !== false; } endif;
使用此功能,您可以按角色或用户ID/电子邮件搜索已登录的用户.还接受用户角色数组.
This is old post but here is one universal function what working on the all WordPress versions.
if(!function_exists('is_user')): function is_user ($role=NULL, $user_id=NULL) { if(empty($user_id)){ $user = wp_get_current_user(); } else { if(is_numeric($user_id) && $user_id == (int)$user_id) { $user = get_user_by('id', (int)$user_id); } else if(is_string($user_id) && $email = sanitize_email($user_id)) { $user = get_user_by('email', $email); } else { return false; } } if(!$user) return false; return in_array( $role, (array)$user->roles, true ) !== false; } endif;
With this function you can search logged in user by role or by user ID/email. Also accept user roles array.
我有一个非常具体的要求,即根据当前用户的角色,在用户个人资料页面的字段标签中显示不同的文本.我似乎无法弄清楚如何检查当前使用的是否是"作者".
我正在寻找类似的功能
我想这很简单,但是我搜索了很长时间却没有答案,所以我想把它发布在这里.