Tri*_* .L 27 php wordpress user-roles
我需要以某种方式检查某人的角色只有他们的身份.我找到了current_user_can()支票.但这仅适用于已登录的用户.如果该用户不是当前用户,我该如何检查?我正在使用电话订购系统,但使用管理员/特定帐户为其他人订购.
谢谢
Mea*_*jay 50
您无法直接获得用户角色.首先,您必须获取user_meta_data,它将返回一个包含用户角色的Object.
码:
$user_meta=get_userdata($user_id);
$user_roles=$user_meta->roles; //array of roles the user is part of.
Run Code Online (Sandbox Code Playgroud)
在继续之前您需要了解的信息:
让我们获取所有角色并检查您感兴趣的角色是在那里还是现在.
<?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';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51693 次 |
| 最近记录: |