I have created a menu and I have the ID.
$menu_id = wp_create_nav_menu( 'Main Menu' );
And I have registered a navigation menu location:
register_nav_menus( array( 'menu-header' => 'Header menu', ) );
How to set "Display location" for this menu?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.Sign up to join this community
You can retrieve nav menu locations with
int Associative array of registered navigation menu IDs keyed by their location name. If none are registered, an empty array.
This refers to a theme mod internally named
nav_menu_locations which can be used to update the locations.
You can modify that returned value and then save the new array like this:
// Update theme mod $locations = get_nav_menu_locations(); $locations['menu-header'] = $menu_id; set_theme_mod( 'nav_menu_locations', $locations );
Note that this won't run filters and actions, caches may not be updated as a result, etc.
I'd also note that if you're trying to create side menus for dynamically generated content that this does not scale, and you will run into issues. There are better ways to show content specific menus and sidebars that do not involve the creation of nav menus.