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.

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() {
  register_nav_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
add comment

1 Answer

up vote 3 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
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.