I want a network of sites in a multisite install to automatically have certain items in the main menu when the user first uses the site.

This seems surprisingly hard to do, and the new REST API doesn't provide ways to interact with menus.

So instead I've created a simple plugin, which creates a menu if it doesn't exist, and adds various items to it.

I now want to make sure that that menu is activated and visible in the primary menu when people come to the site.

I currently can't see a function that activates the menu in a specified location.

Any suggestions how to do it?

1 Answer 1


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.