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...
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...
header-menu
a theme location or a dynamic sidebar? (Your previous comment confused me a bit...)