[英]Get user role by ID WordPress
我需要以某種方式檢查某人的角色只有他們的身份。 我找到了current_user_can()
檢查。 但這僅適用於已登錄的用戶。如果該用戶不是當前用戶,我將如何檢查? 我正在使用電話訂購系統,但使用管理員/特定帳戶為其他人訂購。
您無法直接獲得用戶角色。 首先,您必須獲取user_meta_data,它將返回一個包含用戶角色的Object。
碼:
$user_meta=get_userdata($user_id);
$user_roles=$user_meta->roles; //array of roles the user is part of.
在繼續之前您需要了解的信息:
讓我們獲取所有角色並檢查您感興趣的角色是在那里還是現在。
<?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';
}
$user_meta = get_userdata($user_id);
$user_roles = $user_meta->roles;
if ( in_array( 'administrator', $user_roles, true ) ) {
//echo User is a administrator';
}
你好試試這個最佳代碼
if (get_userdata($post->post_author)->roles[0] == 'your_specified_role'){
echo 'role exist';
}else{
echo 'role does not exist';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.