0

I've been testing the new 3.3 RC1 code against my plugin and I've found a significant difference in the switch_theme() function that foobars my ability to preset widgets dynamically.

The problem appears to be the call to the global $sidebars_widgets. Notice that in prior versions, the switch_theme() method did not involve the sidebar widgets.

Anyone know what the purpose of that is?

I have a plugin that creates and inserts widgets dynamically just before installing a new theme and calling switch_theme() during plugin activation. The code runs fine in 3.2.1, but does not run in 3.3 rc1 (The sidebar widgets are created, just not inserted into the sidebars)

Here's the code from 3.2.1

function switch_theme($template, $stylesheet) {
    global $wp_theme_directories;

    update_option('template', $template);
    update_option('stylesheet', $stylesheet);
    if ( count($wp_theme_directories) > 1 ) {
        update_option('template_root', get_raw_theme_root($template, true));
        update_option('stylesheet_root', get_raw_theme_root($stylesheet, true));
    }
    delete_option('current_theme');
    $theme = get_current_theme();
    if ( is_admin() && false === get_option( "theme_mods_$stylesheet" ) ) {
        $default_theme_mods = (array) get_option( "mods_$theme" );
        add_option( "theme_mods_$stylesheet", $default_theme_mods );
    }
    do_action('switch_theme', $theme);
}

And here's the updated code from 3.3 rc1

function switch_theme($template, $stylesheet) {
    global $wp_theme_directories, $sidebars_widgets;

    if ( is_array( $sidebars_widgets ) )
        set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );

    $old_theme = get_current_theme();

    update_option('template', $template);
    update_option('stylesheet', $stylesheet);

    if ( count($wp_theme_directories) > 1 ) {
        update_option('template_root', get_raw_theme_root($template, true));
        update_option('stylesheet_root', get_raw_theme_root($stylesheet, true));
    }

    delete_option('current_theme');
    $theme = get_current_theme();

    if ( is_admin() && false === get_option( "theme_mods_$stylesheet" ) ) {
        $default_theme_mods = (array) get_option( "mods_$theme" );
        add_option( "theme_mods_$stylesheet", $default_theme_mods );
    }

    update_option( 'theme_switched', $old_theme );
    do_action( 'switch_theme', $theme );
}
1
  • PLEASE stop adding version number tags to your Qs. It's a pain to go through all of the Qs and delete them. THANKS FOR STOPPING THAT!
    – kaiser
    Jan 10, 2012 at 14:31

1 Answer 1

1

Likely your code is somehow breaking on the new widget handling methods, which save old widget layouts when theme switches occur.

http://wordpress.org/news/2011/11/wherefore-art-thou-widgets/

Most likely, you'll have to switch themes before adjusting the widgets in it, since widgets don't survive theme switches anymore, or at least, will be restored from older settings after a theme switch.

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.