php - 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

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!

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!

Share Improve this question edited Jun 2, 2015 at 9:01 Fredrik asked Jun 2, 2015 at 8:45 FredrikFredrik 1831 silver badge8 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 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.

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

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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745260238a4619179.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信