12

Currently I'm making this menu: (Functions.php)

$menuname = 'Top Menu';
$menu_exists = wp_get_nav_menu_object( $menuname );

if( !$menu_exists){
$menu_id = wp_create_nav_menu($menuname);

wp_update_nav_menu_item($menu_id, 0, array(
    'menu-item-title' =>  __('Programme'),
    'menu-item-classes' => 'programme',
    'menu-item-url' => home_url( '/programme/' ), 
    'menu-item-status' => 'publish'));

wp_update_nav_menu_item($menu_id, 0, array(
    'menu-item-title' =>  __('Speakers'),
    'menu-item-classes' => 'speakers',
    'menu-item-url' => home_url( '/speakers/' ), 
    'menu-item-status' => 'publish'));
}

When I activate my theme, what I'm looking to do is: Activating
(source: cubeupload.com)

Selecting the 'Primary Menu' box automatically so when I start this theme I create a menu and make it the primary menu.

How does one do this?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Thovex
  • 322
  • 2
  • 12

1 Answers1

17

You can set the theme_location of the menu programmatically with:

$locations = get_theme_mod('nav_menu_locations');
$locations['primary-menu'] = $term_id_of_menu;
set_theme_mod( 'nav_menu_locations', $locations );

Add this to your functions.php.

mikegertrudes
  • 633
  • 6
  • 12
  • Ah man no, this changes the menu completely from the database. – tinyCoder Feb 20 '17 at 14:13
  • I added an answer, that is based on this answer - which a bit more functionality here: [Set navigation to be active for menu display location](https://wordpress.stackexchange.com/a/403291/128304) – Zeth Mar 02 '22 at 09:39