Remove Customize Background and Header from Appearance admin menu without CSS or JS

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!

Solutions Collecting From Web of "Remove Customize Background and Header from Appearance admin menu without CSS or JS"

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.

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

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