3

I need to add a meta box to the backend menu page, in Appearance->Menus (/wp-admin/nav-menus.php), to list all the available post types, both the defaults & the custom ones.

It will contain a list that contains a link to the archive pages only of the respective post types to be added to the nav menu. Is this possible?

  • What do you define as being the backend menu page? Clarification please. – t31os Oct 6 '11 at 13:31
  • It's the menus editor under Appearances > Menus – Giraldi Oct 7 '11 at 10:11
8

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.

2

As the meta box was already answered by @TomJNowell I only show you how to get the post type list:

/**
 * Builds a html form element list depending on the param type
 * 
 * @param string $type | form element type; Valid: select/checkbox/radio
 * @param bool $echo | (optional) print or return
 * @return string $html
 */
public function get_post_types_list( $type, $echo = false )
{
    $name = ' name="nav-menu-post-types';
    $html = ! in_array( $type, array( 'checkbox', 'radio' ) ? "<{$type}{$name}'>" : '';
    foreach( array_keys( $GLOBALS['wp_post_types'] ) as $pt )
        $html .= 'select' === $type ? "<option>{$pt}</option>" : "<input type='{$type}' {$name}[]' value='{pt}' />";
    $html .= ! in_array( $type, array( 'checkbox', 'radio' ) ? "</{$type}>" : '';

    if ( $echo ) 
        return print $type;
    return $type;
}

It's not tested, but it should work.

1

An alternate to @kaiser's solution is the get_post_types() function. It should allow you a bit finer control over the post types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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