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.

This is my code thus far:

function register_my_menus() {
    register_nav_menus(
        array(
             'header-menu' => __( 'Header Menu' ),
             'extra-menu' => __( 'Extra Menu' )
        )
    );
}
add_action( 'init', 'register_my_menus' );

But this seems to only register 'locations' in which to place custom menus. What I want is for all the custom menus (and menu items) to be fully setup on after_theme_setup or init.

Thanks

share|improve this question
    
Unless you know the exact content etc for your menu there isn't much point in setting up the menu items. The custom menu locations are called in to the theme using wp_nav_menu(); –  Alex Older Apr 25 '12 at 14:44
    
I do know the exact content for the menus, and it needs to be used across many websites. So yeah ideally I'd like it to all be setup when the theme is activated. –  Vertaxe Apr 25 '12 at 15:03
add comment

2 Answers

up vote 1 down vote accepted

Example code taken from new2wp.com located HERE

   // Function for registering wp_nav_menu() in 3 locations
    add_action( 'init', 'register_navmenus' );
    function register_navmenus() {
        register_nav_menus( array(
            'Top'       => __( 'Top Navigation' ),
            'Header'    => __( 'Header Navigation' ),
            'Footer'    => __( 'Footer Navigation' ),
            )
        );

        // Check if Top menu exists and make it if not
        if ( !is_nav_menu( 'Top' )) {
            $menu_id = wp_create_nav_menu( 'Top' );
            $menu = array( 'menu-item-type' => 'custom', 'menu-item-url' => get_home_url('/'),'menu-item-title' => 'Home' );
            wp_update_nav_menu_item( $menu_id, 0, $menu );
        }
        // Check if Header menu exists and make it if not
        if ( !is_nav_menu( 'Header' )) {
            $menu_id = wp_create_nav_menu( 'Header' );
            $menu = array( 'menu-item-type' => 'custom', 'menu-item-url' => get_home_url('/'), 'menu-item-title' => 'Home' );
            wp_update_nav_menu_item( $menu_id, 0, $menu );
        }
        // Check if Footer menu exists and make it if not
        if ( !is_nav_menu( 'Footer' )) {
            $menu_id = wp_create_nav_menu( 'Footer' );
            $menu = array( 'menu-item-type' => 'custom', 'menu-item-url' => get_home_url('/'), 'menu-item-title' => 'Home' );
            wp_update_nav_menu_item( $menu_id, 0, $menu );
        }

        // Get any menu locations that dont have a menu assigned to it and give it on
        /* Currently not working. couldnt fix it.
        $loc = array('Top', 'Header', 'Footer');
        if ( has_nav_menu( $location )) {
            $locations = get_nav_menu_locations();
            return (!empty( $locations[ $location ] ));
        }
        */
    }
    /* Delete nav menu in case you need it
    wp_delete_nav_menu( $menu );
    */
share|improve this answer
add comment

For anyone seeking the same solution, here is the code:

function register_my_menus() {

    $menus = array(
        'Main menu' => array(
            'slug' => 'main-menu',
            'menu_items' => array(
                'Home' => site_url(),
                'Search' => site_url('/search/'),
                'Online now' => site_url('online-now'),
                'Video Chat' => site_url('/video-chat/'),
                'Galleries' => site_url('/galleries/'),
                'Forums' => site_url('/forums/'),
                'Live cams' => site_url('/live-cams/')
            )
        )
    );

    foreach($menus as $menu_title => $menu_var) {
        register_nav_menu( $menu_var['slug'], $menu_title );
        if( !is_nav_menu($menu_title) ) {
            $menu_id = wp_create_nav_menu( $menu_title );
            foreach( $menu_var['menu_items'] as $menu_item_name => $menu_item_url ) {
                $item = array ( 'menu-item-type' => 'custom', 'menu-item-url' => $menu_item_url, 'menu-item-title' => $menu_item_name );
                wp_update_nav_menu_item( $menu_id, 0, $item );
            }
        }
     }
}

add_action( 'init', 'register_my_menus' );

All you need to do is modify the $menus array. :)

share|improve this answer
    
Excellent. I had to make a few changes to get it to work though –  Coombesy yesterday
add comment

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.