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.

I'm getting a puzzling error when I var_dump on $menu_id in the code below...

First, the test code to create a custom menu on the fly and assign it to a registered menu location called "header-menu" (creates the menu but fails in assigning it to the theme's menu location)...

$menu_id = wp_create_nav_menu('my_test_menu_abc_123');
//var_dump($menu_id);die;
wp_update_nav_menu_item($menu_id, 0, array(
    'menu-item-title' => 'First Menu Item', 
    'menu-item-url' => 'http://mysite.com', 
    'menu-item-status' => 'publish'));
$theme = get_current_theme();
$mods = get_option("mods_$theme");
$mods['nav_menu_locations']['header-menu'] = $menu_id;
update_option("mods_$theme", $mods);

When I var_dump on $menu_id below, its returning...

object(WP_Error)#110 (2) { ["errors"]=> array(1) { ["menu_exists"]=> array(1) { [0]=> string(102) "The menu name my_test_menu_abc_123 conflicts with another menu name. Please try another." } } ["error_data"]=> array(0) { } }

It does not matter what I name the menu, I'm always getting this error. Which leads me to believe I have a flaw in the logic of my code below. Although, it still creates the menu fine, and inserts the menu item into it. It just fails to assign the menu to the theme's header-menu location.

Any ideas?

Background: Thanks to the help of @Bainternet, I've managed to come pretty close to a solution. What I'm trying to do is to programmatically create a new custom menu on the fly [solved], then assign that menu to one of my theme's registered "Menu Locations" [still not solved - and no one else appears to be able to do this], all via script inside a plugin.

However, eve though the code above will create the my_test_menu_test perfectly, its not assigning it to the "header-menu" location of the theme.

My first clue to a problem, is the fact that the echo statement appears to be in conflict with the code I'm using to create the menu.

Here is an external reference I've been using...

http://wordpress.org/support/topic/how-to-assign-a-wordpress-3-menu-to-primary-location-programmatically

However, its unclear whether the author's solution actually works for anyone else (according to my experience and that of the other responders on the post)...

Interestingly, as some have noted, the assignment of a custom nav menu to a theme's custom menu locations does not appear to be included in the Custom Menu API...

share|improve this question
    
Defined menu locations are entirely Theme-dependent. If you're doing this via Plugin, how will you know what menu location(s) will be defined to begin with? –  Chip Bennett Apr 7 '11 at 17:19
    
@Chip Bennett: you can register new menu location with a plugin, and even display them by a plugin, using shortcode or virtual pages. –  Bainternet Apr 7 '11 at 17:32
    
@Chip: My theme is currently active on the test site. The theme has two active registered sidebar locations. The first is "header-menu". So I don't expect the menu to hook up unless my theme is installed and active, but if it is, as in this case, I want to go ahead and hook the custom menu I just created, to the "header-menu" theme location. Make sense? –  Scott B Apr 7 '11 at 18:12
    
Wait: is header-menu a theme location or a dynamic sidebar? (Your previous comment confused me a bit...) –  Chip Bennett Apr 7 '11 at 19:23
    
got it, give me a few minutes i'll get home and post the answer. –  Bainternet Apr 7 '11 at 20:41

2 Answers 2

up vote 3 down vote accepted

I got that same error when i put the code you posted in my functions file so i wrapped it up in an if to run only once and i get the menu id using get_term_by and i assign the menu location using set_theme_mod() so here is your very own function to assign a menu to a location:

function scotts_set_nav_menu($menu_id,$location){
    $locations = get_theme_mod('nav_menu_locations');
    $locations[$location] = $menu_id;
    set_theme_mod( 'nav_menu_locations', $locations );
}

and you can try it with this code:

$run_once = get_option('menu_check');
if (!$run_once){
    $name = 'test';
    $menu_id = wp_create_nav_menu($name);
    $menu = get_term_by( 'name', $name, 'nav_menu' );
    wp_update_nav_menu_item($menu->term_id, 0, array(
        'menu-item-title' => 'First Menu Item', 
        'menu-item-url' => 'http://mysite.com', 
        'menu-item-status' => 'publish'));
    scotts_set_nav_menu($menu->term_id,'header-menu');
    update_option('menu_check', true);
}

just tried it and it works.

share|improve this answer
    
YES! YES! YES! It works!!!! +100 –  Scott B Apr 7 '11 at 21:19
    
I've been trying to crack this nut for 3 solid weeks. Thank you! I feel like a pioneer astronaut and you've just talked me through the first lunar landing. I really don't think this has ever been done before today. Who would have guessed the run once issue? –  Scott B Apr 7 '11 at 21:24
    
I'm going to leave it open for a while. You should get some upvotes for this at the very least. Can you get upvotes after a question has been selected as the best answer? –  Scott B Apr 7 '11 at 21:26

You dont need to do:

$menu = get_term_by( 'name', $name, 'nav_menu' );
    wp_update_nav_menu_item($menu->term_id, 0, array(
        'menu-item-title' => 'First Menu Item', 
        'menu-item-url' => 'http://mysite.com', 
        'menu-item-status' => 'publish'));

You can just do:

wp_update_nav_menu_item($menu_id, 0, array(
            'menu-item-title' => 'First Menu Item', 
            'menu-item-url' => 'http://mysite.com', 
            'menu-item-status' => 'publish'));

I mean use the menu_id returned when you create it.

Thanks for the solution anyway i still cant vote up tought

share|improve this answer

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.