WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create a theme which when activated, sets up a primary navigation, adds the homepage to it and then enables it in the correct location.

Here's what I've got so far:

register_nav_menu('Primary', 'Primary Navigation');

$primary_nav_menu_id = wp_create_nav_menu('Primary');

wp_update_nav_menu_item($primary_nav_menu_id, 0, array(
    'menu-item-title' =>  __('Home'),
    'menu-item-classes' => 'home',
    'menu-item-url' => home_url( '/' ), 
    'menu-item-status' => 'publish'

The above creates the menu, adds a link to the homepage but how would I go about automatically assigning this menu a theme location of 'Primary Navigation'?

Is this possible?

share|improve this question

You need to first collect the menu locations, then set the primary menu location with the menu id.

// Set the menu to primary menu location
$locations = get_theme_mod( 'nav_menu_locations' );
$locations['primary'] = $primary_nav_menu_id;
set_theme_mod ( 'nav_menu_locations', $locations );

Here I assume 'primary' is theme location referring to 'Primary Navigation'.

share|improve this answer
The WordPress theme Largo has similar functionality to attempt to link the menus with the theme locations if not set. github.com/INN/Largo/blob/master/inc/nav-menus.php – Seamus Leahy Dec 7 '13 at 3:47

Your Answer


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.