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

The script below creates a nav menu and assigns a page to it. Everything's working perfectly except that the nav menu is not being automatically assigned to my theme's "Header-menu" location.

Any ideas why?

//Register menu locations for the theme
add_action( 'init', 'register_my_menus' );
function register_my_menus() {
    array('header-menu' => __( 'Header Menu' ), 'footer-menu' => __( 'Footer Menu' ))

//Create a nav menu, add a page to it and assign it to the theme's "header-menu" location.
$menu_id = wp_create_nav_menu( 'header-menu' );
$menu = array( 
    'menu-item-type' => 'custom', 
    'menu-item-url' => get_home_url('/'),
    'menu-item-title' => 'Home', 
    'menu-item-status' => 'publish', 
    'theme_location' => 'header-menu', //Just a guess but didn't work!!!
wp_update_nav_menu_item( $menu_id, 0, $menu );
share|improve this question
up vote 4 down vote accepted

wp_create_nav_menu() function accepts the menu name and not the menu location. and as for wp_update_menu_item() well its for items and not the menu itself. After digging in the code i found out that "assignment" of 'theme location' to a menu is not done using of the nav_menu API in WordPress but with theme options so:

//first get the current theme
$theme = get_current_theme();
//get theme's mods
$mods = get_option("mods_$theme");
//update mods with menu id at theme location
$mods['nav_menu_locations']['header-menu'] = $menu_id;
update_option("mods_$theme", $mods);

hope this helps :)

share|improve this answer
You are a rock star. No wonder I couldn't find it. If I had a +20 button, I'd be pressing it hard. – Scott B Apr 7 '11 at 0:56
your suggestion has me much closer to a solution and I'm selecting it as the answer. Its still not assigning the menu to the menu location though. I'm getting some trace errors on $menu_id already existing. So, I'm posting that as a separate question. – Scott B Apr 7 '11 at 16:05

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.