3

I am trying to remove background and header from appearance menu, but they don't seem to go away! I think it's because I have activated customize, but can I remove them anyway without using CSS or JS?

Here is my code:

add_action('admin_menu', 'remove_unnecessary_wordpress_menus', 999);

function remove_unnecessary_wordpress_menus(){
    remove_menu_page('themes.php?page=custom-background');
    remove_submenu_page('themes.php', 'custom-background');
    remove_submenu_page('themes.php', 'custom-header');
}

Thanks in advance!

4

As overcomplicated as it sounds, I always find the best way to handle admin menu modifications is to overlook the given wordpress remove_ functions and go straight to the $menu and $submenu globals. In the case you've specified here, you'd want to change your code to:

add_action('admin_menu', 'remove_unnecessary_wordpress_menus', 999);

function remove_unnecessary_wordpress_menus(){
    global $submenu;
    unset($submenu['themes.php'][20]);
    unset($submenu['themes.php'][22]);
}

The indices of the pages in the themes.php array seem strange, but what isn't when you try to hack WP?! A good reference for using these globals can be found here.

EDIT: Just a thought, given the varying amounts of plugins etc. which could (potentially, but not definitely) change the index of a given menu/submenu item in the array, it'd be a good idea to check the numbers required if the snippet I've provided doesn't work. You can do that by modifying the code slightly to this:

add_action('admin_menu', 'remove_unnecessary_wordpress_menus', 999);

function remove_unnecessary_wordpress_menus(){
    global $submenu;
    //Left margin is to account for the admin sidebar menu
    echo '<pre style="margin-left:11em">';
    print_r($submenu);
    echo '</pre>';
}

This will 'pretty' print the $submenu array, from which you can find the exact numbers you need.

EDIT: Since I don't have the rep to comment on this community yet, it's worth pointing out that @Fredrik did a good job of generalisation. +1.

7

Here is my final of the code. Thanks for the fast answer!

add_action('admin_menu', 'remove_unnecessary_wordpress_menus', 999);

function remove_unnecessary_wordpress_menus(){
    global $submenu;
    foreach($submenu['themes.php'] as $menu_index => $theme_menu){
        if($theme_menu[0] == 'Header' || $theme_menu[0] == 'Background')
        unset($submenu['themes.php'][$menu_index]);
    }
}
  • Simple, effective generalisation and easily customisable (please excuse the pun ;-) – Chris Pink Jun 24 '19 at 9:33
  • Note that this will not work if your WP installation is not in English. In that case you need to replace the words 'Header' and 'Background' with the correct translation. Or even easier, just use: __('Header') and __('Background') instead. – Jules Jan 25 at 20:04
1

Here is another option to remove header and background (source):

//Remove the custom options provided by the default twentyeleven theme.     
add_action( 'after_setup_theme','remove_twentyeleven_options', 100 );
function remove_twentyeleven_options() {    
    remove_custom_background();
    remove_custom_image_header();
    remove_action('admin_menu', 'twentyeleven_theme_options_add_page');    
}
0

Thanks all! Here is the code within WordPress 4.9.8.

function remove_header_and_bg(){
  global $submenu;
  unset($submenu['themes.php'][6]); // customize
  unset($submenu['themes.php'][15]); // header_image
  unset($submenu['themes.php'][20]); // background_image
}
add_action( 'admin_menu', 'remove_header_and_bg', 999 );
0

Another version without need to use a loop is to use the correct identifier, which can be displayed by var_dump the global $submenu.

function remove_unnecessary_wordpress_menus(){
    remove_submenu_page( 'themes.php', 'customize.php?return=%2Fwp-admin%2Findex.php&#038;autofocus%5Bcontrol%5D=header_image' );
    remove_submenu_page( 'themes.php', 'customize.php?return=%2Fwp-admin%2Findex.php&#038;autofocus%5Bcontrol%5D=background_image' );
}

add_action('admin_menu', 'remove_unnecessary_wordpress_menus', 999);

This works only if the path to the website is /, otherwise you need to add the path after customize.php?return=, e. g. customize.php?return=%2Fwordpress%2F if the path is /wordpress/.

It’s also possible to handle the path automatically:

function remove_unnecessary_wordpress_menus(){
    $site_path = rawurlencode( get_blog_details()->path );

    remove_submenu_page( 'themes.php', 'customize.php?return=' . $site_path . 'wp-admin%2Findex.php&#038;autofocus%5Bcontrol%5D=header_image' );
    remove_submenu_page( 'themes.php', 'customize.php?return=' . $site_path . 'wp-admin%2Findex.php&#038;autofocus%5Bcontrol%5D=background_image' );
}

add_action('admin_menu', 'remove_unnecessary_wordpress_menus', 999);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.