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.

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.

share|improve this question
5  
Why don’t you just register a post type for the sections with show_in_nav_menus = TRUE? –  toscho Jun 1 at 12:28
    
That's it! Thanks! –  Toine Jun 2 at 6:59

1 Answer 1

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.

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.