是否删除特定角色的菜单?

时间:2012-03-14 作者:redconservatory

我看到了this snippet 要删除菜单,请执行以下操作:

function remove_menus () {
global $menu;
    $restricted = array(__(\'Dashboard\'), __(\'Posts\'), __(\'Media\'), __(\'Links\'), __(\'Pages\'), __(\'Appearance\'), __(\'Tools\'), __(\'Users\'), __(\'Settings\'), __(\'Comments\'), __(\'Plugins\'));
    end ($menu);
    while (prev($menu)){
        $value = explode(\' \',$menu[key($menu)][0]);
        if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
    }
}
add_action(\'admin_menu\', \'remove_menus\');
是否有一种方法可以限制特定的菜单,但仅限于“编辑角色”?

我试过这样的方法:

$_the_roles = new WP_Roles();
$_the_roles->remove_cap(\'editor\',\'moderate_comments\');
但我仍然可以看到评论菜单。。。

1 个回复
最合适的回答,由SO网友:SolaceBeforeDawn 整理而成

以下是您的答案:

function remove_menus () {
global $menu;

if( (current_user_can(\'install_themes\')) ) { 
$restricted = array(); } // check if admin and hide nothing
else { // for all other users


if ($current_user->user_level < 10)
$restricted = array(__(\'Dashboard\'), __(\'Posts\'), __(\'Media\'), __(\'Links\'), __(\'Pages\'), __(\'Appearance\'), __(\'Tools\'), __(\'Users\'), __(\'Settings\'), __(\'Comments\'), __(\'Plugins\')); // this removes a lot! Just delete the ones you want to keep showing
end ($menu);
while (prev($menu)){
    $value = explode(\' \',$menu[key($menu)][0]);
    if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}
}
}

add_action(\'admin_menu\', \'remove_menus\');
您可以通过调整if((current\\u user\\u can(\'install\\u themes\')参数来调整它以适合用户的任何角色

查看此处了解更多信息http://codex.wordpress.org/Function_Reference/current_user_can

结束