Remove unusable metaboxes in nav menu management screen

There are two important global variables in wp-admin/nav-menus.php:

  • $nav_menus is an array of all available menus, filled with wp_get_nav_menus().
  • $wp_meta_boxes['nav-menus'] is an array of all available metaboxes for this screen.

We can hook into admin_head-nav-menus.php, an action called after the first variable has been set up, and change the second variable:

add_action( 'admin_head-nav-menus.php', 't5_hide_dead_menu_metaboxes' );

/**
 * Remove metaboxes for menu items when no menu exists.
 *
 * @wp-hook admin_head-nav-menus.php
 * @return  void
 */
function t5_hide_dead_menu_metaboxes()
{
    empty ( $GLOBALS['nav_menus'] )
        and $GLOBALS['wp_meta_boxes']['nav-menus'] = array ();
}

Download as plugin T5 Hide dead menu metaboxes from GitHub.

Additional Information:

After removing meta boxes the screen is looking a bit odd. We could change a bit initial hook and add some styles to make it looking better. So our hook could be:

/**
 * Remove metaboxes for menu items when no menu exists.
 *
 * @wp-hook admin_head-nav-menus.php
 * @return  void
 */
function t5_hide_dead_menu_metaboxes()
{
    if ( empty( $GLOBALS['nav_menus'] ) ) {
        $GLOBALS['wp_meta_boxes']['nav-menus'] = array ();
        echo '<style> #nav-menus-frame { margin-left: 0 !important; padding-top: 20px; } </style>';
    }
}

Then the screen will look more natural:

enter image description here

Leave a Comment