Remove menus and submenus

时间:2011-10-16 作者:Alex

所以我找到了一些方便的代码片段来帮助删除管理菜单项。但是,我对子菜单项有问题。我想保留外观菜单,但去掉主题、小部件和编辑器。

function remove_menus()
{
global $menu;
global $current_user;
get_currentuserinfo();
if($current_user->user_login == \'username\')
{
    $restricted = array(__(\'Posts\'),
                        __(\'Links\'),                     
                        __(\'Comments\'),
                        __(\'Plugins\'),
                        __(\'Users\'),
                        __(\'Tools\'),    
                        __(\'Settings\')

    );
    end ($menu);
    while (prev($menu)){
        $value = explode(\' \',$menu[key($menu)][0]);
        if(in_array($value[0] != NULL?$value[0]:"" , $restricted))
       {unset($menu[key($menu)]);}
    }// end while
    }// end if
    }
    add_action(\'admin_menu\', \'remove_menus\');

function remove_submenus()
{
global $menu;
global $current_user;
get_currentuserinfo();
if($current_user->user_login == \'username\')
{
    global $submenu;
    unset($submenu[\'themes.php\'][10]); // remove the theme editor
}
}
add_action(\'admin_menu\', \'remove_menus\');

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

试试这个:

add_action(\'_admin_menu\', \'remove_editor_submenu\', 1);
function remove_editor_submenu() {
    remove_action(\'admin_menu\', \'_add_themes_utility_last\', 101);
}

add_action(\'admin_init\', \'remove_theme_submenus\');
function remove_theme_submenus() {
    global $submenu; 
    unset($submenu[\'themes.php\'][5]);
    unset($submenu[\'themes.php\'][7]);
    unset($submenu[\'themes.php\'][15]);
}
要禁用其他子菜单名称,请转至/wp管理/菜单。php并搜索要禁用的项目。

编辑:至于按用户名禁用,我将为角色添加一个新功能,并将其用作删除条件see here. 否则,只需使用您已经在使用的内容,如下所示:

add_action(\'_admin_menu\', \'remove_editor_submenu\', 1);
function remove_editor_submenu() {
    global $current_user;
    get_currentuserinfo();
    if($current_user->user_login == \'username\') {
        remove_action(\'admin_menu\', \'_add_themes_utility_last\', 101);
    }
}

add_action(\'admin_init\', \'remove_theme_submenus\');
function remove_theme_submenus() {
    global $submenu, $current_user;
    get_currentuserinfo();
    if($current_user->user_login == \'username\') {
        unset($submenu[\'themes.php\'][5]);
        unset($submenu[\'themes.php\'][7]);
        unset($submenu[\'themes.php\'][15]);
    }
}

SO网友:Milo

自3.1以来,删除菜单和子菜单页面的功能更加简单:

remove_menu_page()remove_submenu_page()

然而,删除主题的问题是,外观菜单是主题页面。

编辑-您可以删除整个外观菜单,然后使用创建一个新的顶级菜单项add_menu_page()

function wpse31255_add_menu_page(){
    add_menu_page( __(\'Nav Menus\', \'mav-menus\'), __(\'Nav Menus\', \'nav-menus\'), \'edit_themes\', \'nav-menus.php\', \'\', 99 );
}
add_action(\'admin_menu\', \'wpse31255_add_menu_page\');

结束

相关推荐