如何隐藏特定用户角色,使其对Contributor不可见?
如何限制Contributor角色无法选择特定用户角色?
当然可以实现,直接通过代码过滤可编辑的角色列表即可,以下是具体方案:
- 打开当前主题的
functions.php文件,添加以下代码:
function restrict_contributor_editable_roles($roles) { // 检查当前操作的用户是否为Contributor角色 if (current_user_can('contributor')) { // 定义需要隐藏的角色,比如管理员、编辑等,可根据需求修改 $restricted_roles = array('administrator', 'editor'); // 从可选角色列表中移除受限角色 foreach ($restricted_roles as $role) { if (isset($roles[$role])) { unset($roles[$role]); } } } return $roles; } add_filter('editable_roles', 'restrict_contributor_editable_roles');
- 代码说明:
editable_roles是WordPress提供的钩子,专门用于过滤用户可编辑的角色列表- 函数先判断当前用户是否为Contributor,若是则移除你指定的角色选项
- 你可以根据实际需求修改
$restricted_roles数组,将所有不想让Contributor分配的角色slug(包括自定义角色)添加进去
设置完成后,Contributor在添加或修改用户角色时,就看不到你指定的那些角色选项了。
内容的提问来源于stack exchange,提问作者Courtney Huber
相关产品推荐
相关产品推荐

