How to add meta box to backend menu page

Here is an example metabox that displays at the very top of the left hand side in the nav menus interface:

/**
 * Instantiates the class
 */
add_action( 'admin_init', array( 'call_someClass', 'init' ) );

/**
 * The Class
 */
class call_someClass {
    const LANG = 'exch_lang';

    public static function init() {
    $class = __CLASS__;
    new $class;
    }

    public function __construct() {
    // Abort if not on the nav-menus.php admin UI page - avoid adding elsewhere
    global $pagenow;
    if ( 'nav-menus.php' !== $pagenow )
                    return;

        $this->add_some_meta_box();
    }

    /**
     * Adds the meta box container
     */
    public function add_some_meta_box(){
        add_meta_box(
            'info_meta_box_'
            ,__( 'Example metabox', self::LANG )
            ,array( $this, 'render_meta_box_content' )
            ,'nav-menus' // important !!!
            ,'side' // important, only side seems to work!!!
            ,'high'
        );
    }

    /**
     * Render Meta Box content
     */
    public function render_meta_box_content() {
        echo '<p>Example text</p>';
    }
}

The important part of add_meta_box is:

            ,'nav-menus' // important !!!
            ,'side' // important, only side seems to work!!!

There is a nav-menu post type, but it does not support metaboxes, and nav-menus.php is hardcoded to use the ‘nav-menus’ and ‘side’ values. As long as you respect this you can do anything else you please within reason.

Unfortunately adding extra fields to individual menu items themselves e.g. links, pages, etc, is not possible at the time of writing this as those fields are hardcoded. You could add them via jQuery, and then save them via hooks in the backend if you needed them.

Leave a Comment