Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When you visit wp-admin/nav-menus.php and there is no menu you get a set of metaboxes you cannot use. The only thing they do is drawing attention away from the Create Menu dialog.

enter image description here

I want to hide those boxes until there is a menu.

enter image description here

How can I do that?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.