4

Does anyone know how I can add (custom) modules to Appearance -> Menus? (Or has a link with explanation on how to do this) so that when people change their menu in the backend they can choose what to add to their menu (Pages - Post - Links - Categories - Tags - [custom module])

I ask this question because I want to build a one-page theme. I want to be able to add <section>s to the templates to separate the different 'pages'.
The (rough) idea I have, is create a function which 'scans' trough the templates, isolates the section ID's of these sections, and add them as a menu module to the backend. That way people would be able to add a menu item to one of these sections to their menus.

  • 5
    Why don’t you just register a post type for the sections with show_in_nav_menus = TRUE? – fuxia Jun 1 '14 at 12:28
2

When you register a custom post type, set the argument show_in_nav_menus to TRUE, and WordPress will create the box for you automatically. This works for custom taxonomies too.

Example, requires PHP 5.4 or newer.

add_action( 'wp_loaded', function() {

    $post_type = 'foo';

    add_action( 'registered_post_type', function( $registered ) use ( $post_type ) {

        if ( $registered !== $post_type )
            return;

        register_taxonomy(
            'bar',
            $post_type,
            [
                'label'             => 'Bars',
                'public'            => TRUE,
                'show_in_nav_menus' => TRUE
            ]
        );
    });

    register_post_type(
        $post_type,
        [
            'label'             => 'Foo',
            'public'            => TRUE,
            'show_in_nav_menus' => TRUE
        ]
    );
});

Adding a custom box for anything else is more difficult. Well, adding it is easy, it is just a metabox with the parameter $screen set to nav-menus:

add_action( 'admin_init', function() {

    add_meta_box(
        'test',
        'Custom Box',
        function() {
            print 'Insert content here.';
        },
        'nav-menus',
        'side',
        'low'
    );
});

The difficult part are the following actions: creating the proper HTML for the list items, saving those items with AJAX requests and rendering them in a nav menu on front-end. Inspect the built-in boxes for the details. Repeating and explaining that would require a novel.

The out put of both code examples should look like this:

enter image description here

You can see the boxes for the post type Foo, the taxonomy Bars and the Custom Box. Form is one of my own post types on that installation, Location on of my taxonomies, and Languages a custom box for all available languages in a multilingual network built with Multilingual Press.

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.