如何在wordpress插件中獲取當前用戶相對於角色的權限;如何根據當前用戶角色獲取權限
如: -
如果輸入editor
函數將返回delete_others_posts
,如果輸入的是author
結果將是delete_published_posts
等等...提前
如何在wordpress插件中獲取當前用戶相對於角色的權限;如何根據當前用戶角色獲取權限
如: -
如果輸入editor
函數將返回delete_others_posts
,如果輸入的是author
結果將是delete_published_posts
等等...提前
由於獲取的名稱當前用戶角色:
function get_current_user_role() {
global $current_user;
return array_shift($current_user->roles);
}
然後使用get_role($ role_name)func你可以用他所有的能力獲得陣列。
EG:$capabilities = get_current_user_role();
另一種解決方案是使用current_user_can($ capability_name)函數返回true或false
對於整個說明:http://tomarea.fr/wordpress-roles-capacites-utilisateurs/
爲了詳細說明koma182的回答,包get_role()
與var_dump
或print_r
,您將看到當前用戶角色的功能:
function get_current_user_role() {
global $current_user;
return array_shift($current_user->roles);
}
,然後在那裏你調用該函數:
var_dump(get_role(get_current_user_role()));
感謝救命!
你打算怎麼做,你可以進一步解釋 – Gowri