Worked it out.
Assuming using a theme which has a menu location called ‘primary’, and you have just created a menu with an id contained in $menu_id
$menu_locations = get_nav_menu_locations();
$new_menu_locations = array_map( 'absint', ['primary' => $menu_id]);
$menu_locations = array_merge( $menu_locations, $new_menu_locations );
set_theme_mod( 'nav_menu_locations', $menu_locations );
This is pretty much exactly how WordPress itself saves updated menu locations in wp-admin/nav-menus.php
Related Posts:
- Does WP REST API have a built in route for calling menu?
- Extend WordPress REST API to show slugs for menu items
- Reuse a custom WordPress menu on another WordPress website
- New menu is not showing in api response
- Get Menu Items from REST API (React)
- Custom search filter causes menu and query_posts problems
- Add separator to admin submenu
- register_nav_menus() won’t register menus
- How do I add nested categories to drop-down menu in twenty-eleven?
- Add custom page link with anchor
- How to include the ‘current-menu-ancestor’ class on a custom post type menu in WordPress?
- HTML Bootstrap navigation menu to WordPress menu using wp_nav_menu
- Getting URL value from menu item by ID
- Show top-level parent with submenu menu walker
- Add section to custom menu panel
- Adding a class (arrows) to main menu links that have children?
- Is it possible to add a filter for the output of nav-menu items in admin?
- How to change menu according to the language?
- add_action for saving a custom menu
- Highlight the “Home” / “Blog” button while on main page
- Getting post content within wp_nav_walker
- Navigation (wp_nav_menu) refuses to nest within parent element
- how to add Custom menu item like User name in Header Menu only
- Display three different levels of navigation from wp_nav_menu separately?
- Wp_Nav_Menu – Identify the last menu item
- Add custom menu item metabox in Navigation Settings
- Menus – Open items in new tab/window?
- Image as a link using wp_nav_menu()
- Highlight another nav item
- Populating main menu from external JSON API
- HTML in Menu Items disappear on save
- Get category ID from nav menu
- How to put a Menu on the left side of a particular page in WordPress 3.5
- How to make link to different categories that in them selves loads different menus?
- How do you cope with the task of adding hierarchy to the navigation menu?
- Programmatically add existing pages and subpages to Appearance Menu
- Highlight current post ancestor parent menu items
- Restrict control in nav-menu for specific role
- walker – 0 level on submenu formatting
- Ad custom admin page like WordPress customizer [closed]
- Appearance Menus Screen – menu item drop down menu not functioning
- Weird behaviour on Edit Menus screen
- Remove nav menu item by script
- Custom Menu Broken WP 3.8.1 Update
- How to actually make menu walker alive on admin interface?
- Get menus created with menu editor?
- How to I know the class or ID of my theme?
- Highlight parent if item from submenu is active
- How to exclude pages in the menu without pageID?
- Missunderstanding of WordPress menus
- How to add schema.org SiteNavigationElement and property URL to navbar?
- Get custom classes in WordPress Navigation Menu
- changing host.. data loss
- Theme Location empty
- Replace the slug of parent pages with # [closed]
- How to buid wordpress menu with auto width sub-menu items in dropdown menu without any items sharing one line [closed]
- Page appears after de registering from menu
- wp_nav_menu: Is there a way to modify the output of that got a submenu?
- How can I create a new menu item that links to an archive of a custom post type?
- If current menu item is child or parent with child?
- Is it possible to dynamically register menus?
- Dynamic menu wp_list_pages displaying only current branch including: current page, his siblings and its childs
- Opening Menu link in another tab
- How to add class to anchor using wp_nav_menu
- displaying meta key value with wp_page_menu
- WordPress Menus – How to add inline styles to UL menus
- Is it possible to link to the same page from wordpress page menu?
- Remove current-menu-item for a specific link
- removing the list container from the custom menu output
- WordPress website – Mobile menu behavior help
- How to work with a custom theme that someone else created before me? (details below)
- changin the url (permalink as it goes these days) on one specific menu layer
- Custom menu items in admin
- WordPress database issues – menu not saving correctly, media thumbnails not showing
- WordPress Accessible Keyboard Menus
- Parent link need to go first child submenu
- mysterious menu
- Importing large product categories into wordpress menu
- Different HTML markup for similar menus
- Users are required to login to my site. How define user.member boolean, its permissions, and conditionally display/handle behavior based on it
- How to Use Default Auto-Generated Menu?
- How to sort a sub-menu, generated with get_pages(), by the page order instead of alphabetically?
- wp-nav menu with custom walker , how to remove a tag if it has any submenu
- Two questions about CSSing inside the menus
- Different menu on different category need advice
- Always show wordpress submenu on responsive header menu
- Is there any way to recover deleted menu from database sql backup
- Placing generated list into drop-down on custom menu
- Add tooltips with image in WordPress Menu?
- How to make a drop down menu with categories
- How to use my custom menus as seperate pages?
- WordPress wp get nav menu items does not deliver external link
- wp_nav_menu changed to wp_page_menu
- how to assign an ID to a wordpress menu
- Site pages show the page title under navigation bar
- Keeping my Sub Menu open when hovering on its list items
- How to disable permalink of a particular menu item?
- One item on menu is not clickable when it should be
- Critical Error when editing menu
- I need to change the hyperlink attached to the logo on the mobile menu