Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

A site is using "child1" named child theme. When this site switches to other child theme named "child2", their widgets are moving too but theme locations are not moving. How can i auto assign menus to theme locations on theme activation?

I found this solution from

$theme = get_current_theme();
$mods = get_option("mods_$theme");
$key = key($mods['nav_menu_locations']);
$mods['nav_menu_locations'][$key] = $menu_id;
update_option("mods_$theme", $mods);

Is there any better wordpress way to fix this? (Related trac topic: )

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Well, I wrote a solution so writing it here:

 This action copies old theme's theme location saves to 
 new theme if new theme doesnt have saves before.
 add_action( 'after_switch_theme',  'ajx_theme_locations_rescue' );
 function ajx_theme_locations_rescue() {
    // bug report / support:
    // We got old theme's slug name
    $old_theme = get_option( 'theme_switched' );
    // Getting old theme's settings
    $old_theme_mods = get_option("theme_mods_{$old_theme}");
    // Getting old theme's theme location settings
    $old_theme_navs = $old_theme_mods['nav_menu_locations'];
    // Getting new theme's theme location settings
    $new_theme_navs = get_theme_mod( 'nav_menu_locations' );

    // If new theme's theme location is empty (its not empty if theme was activated and set some theme locations before)
    if (!$new_theme_navs) {
        // Getting registered theme locations on new theme
        $new_theme_locations = get_registered_nav_menus();

        foreach ($new_theme_locations as $location => $description ) {
            // We setting same nav menus for each theme location 
            $new_theme_navs[$location] = $old_theme_navs[$location];

        set_theme_mod( 'nav_menu_locations', $new_theme_navs );

share|improve this answer
i tried all possibilities and it seems working but please if u want to use it, try as much as possible and if its working or not, write here. – Ünsal Korkmaz Jul 12 '12 at 16:19

Your Answer


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.