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 );

1 Answer 1


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 :)

  • 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, 2011 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, 2011 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.