Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to set the following option from a WordPress plugin, if you know the name of the menu you want to set: enter image description here

My developer says it's not possible, but I am sure on of you gurus knows a way around this ;)

share|improve this question
    
If you could configure everything theme i wonder why you are looking for a plugin ! –  Raja67 Jan 25 at 13:55
    
It's certainly possible to customize this, but plugin recommendations are off-topic on this site. –  Wyck Jan 25 at 15:01
    
It's not about what plugin to use for this. I am developing a plugin, which imports pages and menus created in another app. I want to activate the imported menu directly from my plugin, so the user does not need to change it manually. –  Paul Jan 26 at 2:48
    
Paul, you will need to post the code you are trying to use to do this, even if that code doesn't work. –  s_ha_dum Jan 30 at 3:39
add comment

2 Answers 2

up vote 1 down vote accepted

You can use the wp_nav_menu_args filter ( Codex reference ) to set a theme location to use a specific menu.

Example:

function test_wp_nav_menu_args( $args = '' ) {
    // only set menu for the primary navigation menu.
    if ( $args['theme_location'] != 'primary' ) {
        return $args;
    }
    // change {main-menu} to be the slug of the menu you want to set.
    $args['menu'] = 'main-menu';
    return $args;
}
add_filter( 'wp_nav_menu_args', 'test_wp_nav_menu_args' );
share|improve this answer
add comment

You can use get_theme_mod and set_theme_mod to save nav menu locations.

Get locations:

$locations = get_theme_mod( 'nav_menu_locations' );
var_dump( $locations );

prints:

Array
(
    [primary] => 0
    [secondary] => 0
)

Get existing nav menus:

$nav_menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
var_dump( $nav_menus );

prints:

Array
(
    [0] => stdClass Object
        (
            [term_id] => 2
            [name] => Main
            [slug] => main
            [term_group] => 0
            [term_taxonomy_id] => 2
            [taxonomy] => nav_menu
            [description] => 
            [parent] => 0
            [count] => 3
        )
}

Set location:

$nav_menu_id = $nav_menus[0]->term_id;
$location = 'primary';
$locations[ $location ] = $nav_menu_id;
set_theme_mod( 'nav_menu_locations', $locations );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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