I am not sure if it is much different for any page but I am wondering how to remove a metabox from the appearance>menu page without having to go into the wordpress core files and remove it. Is there something I can do to override it so it doesn't appear both on the page and on the screen options menu?
By inspecting the file /wp-admin/nav-menus.php
we can see that these meta-boxes:
are rendered with:
<?php do_meta_boxes( 'nav-menus', 'side', null ); ?>
The file /wp-admin/includes/nav-menu.php
contains the corresponding add_meta_box()
calls and from that we can construct the relevant removal code:
function custom_remove() {
remove_meta_box('nav-menu-theme-locations', 'nav-menus', 'side');
remove_meta_box('add-custom-links', 'nav-menus', 'side');
remove_meta_box('add-post', 'nav-menus', 'side');
remove_meta_box('add-page', 'nav-menus', 'side');
remove_meta_box('add-category', 'nav-menus', 'side');
}
add_action('admin_head-nav-menus.php', 'custom_remove');
where we can use for example the admin_head-nav-menus.php
action since it is called before these meta-boxes are rendered.
If you have a custom post type (or a custom taxonomy) then you can remove it's meta-box with
remove_meta_box( 'add-xxx' , 'nav-menus' , 'side' );
where xxx
is the corresponding name.
You can further check out the functions wp_nav_menu_setup()
, wp_nav_menu_post_type_meta_boxes()
and wp_nav_menu_taxonomy_meta_boxes()
to see how the meta-boxes are added.
-
1
if (!is_admin()) return;
// See https://developer.wordpress.org/reference/functions/wp_nav_menu_taxonomy_meta_boxes/
// $taxonomy_object is of type WP_Post_Type
add_filter('nav_menu_meta_box_object', function ($taxonomy_object) {
if (in_array($taxonomy_object->name, ['page', 'post', 'category'] )) {
return null;
}
return $taxonomy_object;