10
Register And Create New Nav Menus With Default Menu Items
This is a function I wrote that will create menu locations and matching menus with custom menu items by using functions built into the menu system for creating menus from the dashboard.
This is useful for theme developers that make themes using wp_nav_menu(‘menu’ => ‘Menu Name’) to create the menus along with the theme so there aren’t confused people installing your theme wondering why the menus are filling up with the default items of an empty or non-existent menu. You can be sure that the menus are created and have at least one item in them this way.
// 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 );
*/






User Comments
( ADD YOURS )Francesco Rizzi August 28
Nice, thanks for the tip; I was curious: did you make the menu item and then call wp_update_nav_menu_item with $menu_item_db_id set to 1 instead of calling wp_update_nav_menu_item with the id set to 0 for any reason in particular?
Jared August 29
I don't really remember. When I wrote that, I honestly had no idea what I was doing haha. I just know that what I did, worked the way I wanted it to, not to say it didn't have flaws. If you can suggest a better way, please share. :)
sebastien133 February 15
Very good post. Thank you !!!
A detail :
wp_update_nav_menu_item( $menu_id, 1, $menu );
SHOUD BE
wp_update_nav_menu_item( $menu_id, 0, $menu );
Regards,
Sebastien.
sebastien April 5
There is no name in the field "menu name". Is there a way to add automaticly the name of the menu in the field "menu name" ? Thanks.
sebastien April 5
and, how can i add more than one item in a menu created with your function ?
Jared April 5
Yes there is but I am sure there is documentation on this by now. I wrote this script last june and had written some half working code that would create the menu name, assign it to a location, and create some default menu items to start with. That way this script could be used in a theme and auto create things when the theme gets installed, but it was never fully working and there was NO documentation on it back then, what-so-ever. I was digging deeeep within the wordpress source to figure out how to do things like this.
I suggest searching for it on WP.org. There is likely someone somewhere that has asked about this same thing.
sebastien April 5
i search but i don't find... :-/
sebastien April 5
to add another item i do like that :
$menu1 = array(
'menu-item-object-id' => 0,
'menu-item-object' => '',
'menu-item-parent-id' => 0,
'menu-item-position' => 0,
'menu-item-type' => 'custom',
'menu-item-title' => 'Accueil' ,
'menu-item-url' => '/yyy',
'menu-item-description' => '',
'menu-item-attr-title' => '',
'menu-item-status' => 'publish'
);
wp_update_nav_menu_item( $menu_id, 0, $menu1 );
$menu2 = array(
'menu-item-object-id' => 0,
'menu-item-object' => '',
'menu-item-parent-id' => 0,
'menu-item-position' => 0,
'menu-item-type' => 'custom',
'menu-item-title' => 'toto' ,
'menu-item-url' => '/yyy',
'menu-item-description' => '',
'menu-item-attr-title' => '',
'menu-item-status' => 'publish'
);
wp_update_nav_menu_item( $menu_id, 0, $menu2 );
but i don't know how add the name of the menu in the field "name menu"
Jared April 5
Sorry, but you searched for not even 5 minutes. I spent months and months searching and digging through the entire source code of WordPress to come up with the code to do this.
I can't help you if you don't even bother to spend some time trying figure it out, or find something that can help you.
Scott April 5
I'm using this code to successfully create a custom menu. However, where I'm getting stuck is assigning the custom menu to one of my theme's registered nav menu locations.
For example, my theme registers 2 nav menus...
register_nav_menus(
array('header-menu' => __( 'Header Menu' ), 'footer-menu' => __( 'Footer Menu' ))
);
And I'm using your code and replacing "Top" with "header-menu", however even though its creating the custom menu, its not assigning it to the header location of the theme's "Header Menu".
Any ideas?
Trackbacks