Remove menus and submenus

So I found some handy snippets to help remove admin menu items. However, I’m having trouble with the sub menu items. I want to keep the appearance menu, but get rid of the Themes, Widgets, and Editor.

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');

Related posts

Leave a Reply

2 comments

  1. Try this:

    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]);
    }
    

    To disable other submenu names, go to ./wp-admin/menu.php and search for the item(s) you want to disable.

    EDIT: As far as disabling by username, I would instead add a new capability to a role and use that as your removal condition see here. Otherwise, just use what you already were using, like so:

    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]);
        }
    }
    
  2. There are much simpler functions for removing menu and submenu pages since 3.1:

    remove_menu_page() and remove_submenu_page()

    however, the issue with removing themes is that the appearance menu IS the themes page.

    EDIT- what you can do is remove the entire appearance menu, then create a new top level menu item using 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');