通过ID WordPress获取用户角色

时间:2016-04-19 14:12:58

标签: php wordpress user-roles

我需要以某种方式检查某人的角色,只有他们的身份证明。 我找到了current_user_can()支票。但这仅适用于已登录的用户。如果该用户不是当前用户,我将如何检查?我正在使用电话订购系统,但使用管理员/特定帐户为其他人订购。

谢谢

4 个答案:

答案 0 :(得分:40)

您无法直接获得用户角色。首先,您必须获取user_meta_data,它将返回一个包含用户角色的Object。

<强>代码:

$user_meta=get_userdata($user_id);

$user_roles=$user_meta->roles; //array of roles the user is part of.

答案 1 :(得分:6)

在继续操作之前您需要了解的信息:

  • 您不能直接通过ID获得用户角色。
  • 可以获得分配给用户的所有角色。

让我们获得所有角色,并检查您感兴趣的角色是否存在。

<?php

// Get the user object.
$user = get_userdata( $user_id );

// Get all the user roles as an array.
$user_roles = $user->roles;

// Check if the role you're interested in, is present in the array.
if ( in_array( 'subscriber', $user_roles, true ) ) {
    // Do something.
    echo 'YES, User is a subscriber';
}

答案 2 :(得分:1)

prd = c(1, 2, 5, 3, 5)
period_variable <- quote(prd)

tdata2 <- tdata %>%
dplyr::group_by(group) %>%
tidyr::complete(prd= tidyr::full_seq(eval(period_variable), period = 1)) %>%
dplyr::ungroup()

答案 3 :(得分:0)

您好尝试一下此最佳代码

if (get_userdata($post->post_author)->roles[0] == 'your_specified_role'){
   echo 'role exist';
}else{
   echo 'role does not exist';
}
相关问题