Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically setting a menu on location 'primary menu' on theme activation

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?

like image 851
Thovex Avatar asked Oct 16 '13 10:10

Thovex


1 Answers

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.

like image 74
mikegertrudes Avatar answered Sep 20 '22 18:09

mikegertrudes