2011-10-13 126 views
0

如何在wordpress插件中获取当前用户相对于角色的权限;如何根据当前用户角色获取权限

如: -

如果输入editor函数将返回delete_others_posts,如果输入的是author结果将是delete_published_posts等等...提前

+0

你打算怎么做,你可以进一步解释 – Gowri

回答

1

由于获取的名称当前用户角色:

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/

0

为了详细说明koma182的回答,包get_role()var_dumpprint_r,您将看到当前用户角色的功能:

function get_current_user_role() { 
    global $current_user; 
    return array_shift($current_user->roles); 
} 

,然后在那里你调用该函数:

var_dump(get_role(get_current_user_role())); 

感谢救命!