Wordpress - Remove Customize Background and Header from Appearance admin menu without CSS or JS
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]);
}
}
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.
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 );