I've been trying to update my theme location programmtically and while the menu gets created with menu items, the theme location never gets set.
Here is what I have:
function create_my_menu() {
if(!is_nav_menu('primary-menu')) {
$menu_id = wp_create_nav_menu('primary-menu');
//$menu = array( 'menu-item-type' => 'custom', 'menu-item-url' => get_home_url('/'),'menu-item-title' => 'Home', 'menu-item-status' => 'publish' );
$menu = get_term_by('name', 'primary-menu', 'nav_menu');
wp_update_nav_menu_item($menu->term_id, 0, array( 'menu-item-type' => 'custom', 'menu-item-url' => get_home_url('/'),'menu-item-title' => 'Home', 'menu-item-status' => 'publish' ));
// wp_update_nav_menu_item( $menu_id, 0, $menu );
$locations = get_theme_mod('nav_menu_locations');
$locations['primary-menu'] = $menu->term_id.
set_theme_mod('nav_menu_locations', $locations);
}
}
My menu is registered in my functions file.
add_action( 'init', 'register_my_menu' );
function register_my_menu() {
register_nav_menu( 'primary-menu', __( 'Primary Menu' ) );
}
EDIT
I'm working on a plugin for a multisite setup. I need the menu to be created when a user creates a new site.
I suppose I could modify the default menu that gets created when wordpress is installed.