admin管理员组

文章数量:1287592

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.

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

How can I do that?

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.

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

How can I do that?

Share Improve this question asked Jan 2, 2013 at 8:30 fuxiafuxia 107k38 gold badges255 silver badges459 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

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:

本文标签: wp adminRemove unusable metaboxes in nav menu management screen