switch_theme

The timeline below displays how wordpress function switch_theme has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backward compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 * @global array                $wp_registered_sidebars
 *
 * @param string $stylesheet Stylesheet name.
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars;
    $requirements = validate_theme_requirements($stylesheet);
    if (is_wp_error($requirements)) {
        wp_die($requirements);
    }
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $old_sidebars_widgets_data_setting = $wp_customize->get_setting('old_sidebars_widgets_data');
        if ($old_sidebars_widgets_data_setting) {
            $_sidebars_widgets = $wp_customize->post_value($old_sidebars_widgets_data_setting);
        }
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $nav_menu_locations = get_theme_mod('nav_menu_locations');
    update_option('theme_switch_menu_locations', $nav_menu_locations);
    if (func_num_args() > 1) {
        $stylesheet = func_get_arg(1);
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    $template = $new_theme->get_template();
    if (wp_is_recovery_mode()) {
        $paused_themes = wp_paused_themes();
        $paused_themes->delete($old_theme->get_stylesheet());
        $paused_themes->delete($old_theme->get_template());
    }
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        if (!empty($nav_menu_locations) && empty($default_theme_mods['nav_menu_locations'])) {
            $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
        }
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else if ('wp_ajax_customize_save' === current_action()) {
        remove_theme_mod('sidebars_widgets');
    }
    // Stores classic sidebars for later use by block themes.
    if ($new_theme->is_block_theme()) {
        set_theme_mod('wp_classic_sidebars', $wp_registered_sidebars);
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /*
     * Reset template globals when switching themes outside of a switched blog
     * context to ensure templates will be loaded from the new theme.
     */
    if (!is_multisite() || !ms_is_switched()) {
        wp_set_template_globals();
    }
    // Clear pattern caches.
    if (!is_multisite()) {
        $new_theme->delete_pattern_cache();
        $old_theme->delete_pattern_cache();
    }
    // Set autoload=no for the old theme, autoload=yes for the switched theme.
    $theme_mods_options = array('theme_mods_' . $stylesheet => 'yes', 'theme_mods_' . $old_theme->get_stylesheet() => 'no');
    wp_set_option_autoload_values($theme_mods_options);
    /**
     * Fires after the theme is switched.
     *
     * See {@see 'after_switch_theme'}.
     *
     * @since 1.5.0
     * @since 4.5.0 Introduced the `$old_theme` parameter.
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     * @param WP_Theme $old_theme WP_Theme instance of the old theme.
     */
    do_action('switch_theme', $new_name, $new_theme, $old_theme);
}

WordPress Version: 4.2

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backward compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 * @global array                $wp_registered_sidebars
 *
 * @param string $stylesheet Stylesheet name.
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars;
    $requirements = validate_theme_requirements($stylesheet);
    if (is_wp_error($requirements)) {
        wp_die($requirements);
    }
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $old_sidebars_widgets_data_setting = $wp_customize->get_setting('old_sidebars_widgets_data');
        if ($old_sidebars_widgets_data_setting) {
            $_sidebars_widgets = $wp_customize->post_value($old_sidebars_widgets_data_setting);
        }
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $nav_menu_locations = get_theme_mod('nav_menu_locations');
    update_option('theme_switch_menu_locations', $nav_menu_locations);
    if (func_num_args() > 1) {
        $stylesheet = func_get_arg(1);
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    $template = $new_theme->get_template();
    if (wp_is_recovery_mode()) {
        $paused_themes = wp_paused_themes();
        $paused_themes->delete($old_theme->get_stylesheet());
        $paused_themes->delete($old_theme->get_template());
    }
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        if (!empty($nav_menu_locations) && empty($default_theme_mods['nav_menu_locations'])) {
            $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
        }
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else if ('wp_ajax_customize_save' === current_action()) {
        remove_theme_mod('sidebars_widgets');
    }
    // Stores classic sidebars for later use by block themes.
    if ($new_theme->is_block_theme()) {
        set_theme_mod('wp_classic_sidebars', $wp_registered_sidebars);
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    // Clear pattern caches.
    $new_theme->delete_pattern_cache();
    $old_theme->delete_pattern_cache();
    /**
     * Fires after the theme is switched.
     *
     * See {@see 'after_switch_theme'}.
     *
     * @since 1.5.0
     * @since 4.5.0 Introduced the `$old_theme` parameter.
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     * @param WP_Theme $old_theme WP_Theme instance of the old theme.
     */
    do_action('switch_theme', $new_name, $new_theme, $old_theme);
}

WordPress Version: 6.4

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backward compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 * @global array                $wp_registered_sidebars
 * @global string               $wp_stylesheet_path
 * @global string               $wp_template_path
 *
 * @param string $stylesheet Stylesheet name.
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars, $wp_stylesheet_path, $wp_template_path;
    $requirements = validate_theme_requirements($stylesheet);
    if (is_wp_error($requirements)) {
        wp_die($requirements);
    }
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $old_sidebars_widgets_data_setting = $wp_customize->get_setting('old_sidebars_widgets_data');
        if ($old_sidebars_widgets_data_setting) {
            $_sidebars_widgets = $wp_customize->post_value($old_sidebars_widgets_data_setting);
        }
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $nav_menu_locations = get_theme_mod('nav_menu_locations');
    update_option('theme_switch_menu_locations', $nav_menu_locations);
    if (func_num_args() > 1) {
        $stylesheet = func_get_arg(1);
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    $template = $new_theme->get_template();
    if (wp_is_recovery_mode()) {
        $paused_themes = wp_paused_themes();
        $paused_themes->delete($old_theme->get_stylesheet());
        $paused_themes->delete($old_theme->get_template());
    }
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        if (!empty($nav_menu_locations) && empty($default_theme_mods['nav_menu_locations'])) {
            $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
        }
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else if ('wp_ajax_customize_save' === current_action()) {
        remove_theme_mod('sidebars_widgets');
    }
    // Stores classic sidebars for later use by block themes.
    if ($new_theme->is_block_theme()) {
        set_theme_mod('wp_classic_sidebars', $wp_registered_sidebars);
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /*
     * Reset globals to force refresh the next time these directories are
     * accessed via `get_stylesheet_directory()` / `get_template_directory()`.
     */
    $wp_stylesheet_path = null;
    $wp_template_path = null;
    // Clear pattern caches.
    $new_theme->delete_pattern_cache();
    $old_theme->delete_pattern_cache();
    /**
     * Fires after the theme is switched.
     *
     * See {@see 'after_switch_theme'}.
     *
     * @since 1.5.0
     * @since 4.5.0 Introduced the `$old_theme` parameter.
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     * @param WP_Theme $old_theme WP_Theme instance of the old theme.
     */
    do_action('switch_theme', $new_name, $new_theme, $old_theme);
}

WordPress Version: 6.3

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backward compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 * @global array                $wp_registered_sidebars
 *
 * @param string $stylesheet Stylesheet name.
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars;
    $requirements = validate_theme_requirements($stylesheet);
    if (is_wp_error($requirements)) {
        wp_die($requirements);
    }
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $old_sidebars_widgets_data_setting = $wp_customize->get_setting('old_sidebars_widgets_data');
        if ($old_sidebars_widgets_data_setting) {
            $_sidebars_widgets = $wp_customize->post_value($old_sidebars_widgets_data_setting);
        }
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $nav_menu_locations = get_theme_mod('nav_menu_locations');
    update_option('theme_switch_menu_locations', $nav_menu_locations);
    if (func_num_args() > 1) {
        $stylesheet = func_get_arg(1);
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    $template = $new_theme->get_template();
    if (wp_is_recovery_mode()) {
        $paused_themes = wp_paused_themes();
        $paused_themes->delete($old_theme->get_stylesheet());
        $paused_themes->delete($old_theme->get_template());
    }
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        if (!empty($nav_menu_locations) && empty($default_theme_mods['nav_menu_locations'])) {
            $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
        }
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else if ('wp_ajax_customize_save' === current_action()) {
        remove_theme_mod('sidebars_widgets');
    }
    // Stores classic sidebars for later use by block themes.
    if ($new_theme->is_block_theme()) {
        set_theme_mod('wp_classic_sidebars', $wp_registered_sidebars);
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /**
     * Fires after the theme is switched.
     *
     * See {@see 'after_switch_theme'}.
     *
     * @since 1.5.0
     * @since 4.5.0 Introduced the `$old_theme` parameter.
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     * @param WP_Theme $old_theme WP_Theme instance of the old theme.
     */
    do_action('switch_theme', $new_name, $new_theme, $old_theme);
}

WordPress Version: 6.2

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backward compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 * @global array                $wp_registered_sidebars
 *
 * @param string $stylesheet Stylesheet name.
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars;
    $requirements = validate_theme_requirements($stylesheet);
    if (is_wp_error($requirements)) {
        wp_die($requirements);
    }
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $old_sidebars_widgets_data_setting = $wp_customize->get_setting('old_sidebars_widgets_data');
        if ($old_sidebars_widgets_data_setting) {
            $_sidebars_widgets = $wp_customize->post_value($old_sidebars_widgets_data_setting);
        }
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $nav_menu_locations = get_theme_mod('nav_menu_locations');
    update_option('theme_switch_menu_locations', $nav_menu_locations);
    if (func_num_args() > 1) {
        $stylesheet = func_get_arg(1);
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    $template = $new_theme->get_template();
    if (wp_is_recovery_mode()) {
        $paused_themes = wp_paused_themes();
        $paused_themes->delete($old_theme->get_stylesheet());
        $paused_themes->delete($old_theme->get_template());
    }
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        if (!empty($nav_menu_locations) && empty($default_theme_mods['nav_menu_locations'])) {
            $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
        }
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else if ('wp_ajax_customize_save' === current_action()) {
        remove_theme_mod('sidebars_widgets');
    }
    // Stores classic sidebars for later use by block themes.
    if ($new_theme->is_block_theme()) {
        set_theme_mod('wp_classic_sidebars', $wp_registered_sidebars);
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /**
     * Fires after the theme is switched.
     *
     * @since 1.5.0
     * @since 4.5.0 Introduced the `$old_theme` parameter.
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     * @param WP_Theme $old_theme WP_Theme instance of the old theme.
     */
    do_action('switch_theme', $new_name, $new_theme, $old_theme);
}

WordPress Version: 5.5

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backward compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 *
 * @param string $stylesheet Stylesheet name.
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets;
    $requirements = validate_theme_requirements($stylesheet);
    if (is_wp_error($requirements)) {
        wp_die($requirements);
    }
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $old_sidebars_widgets_data_setting = $wp_customize->get_setting('old_sidebars_widgets_data');
        if ($old_sidebars_widgets_data_setting) {
            $_sidebars_widgets = $wp_customize->post_value($old_sidebars_widgets_data_setting);
        }
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $nav_menu_locations = get_theme_mod('nav_menu_locations');
    update_option('theme_switch_menu_locations', $nav_menu_locations);
    if (func_num_args() > 1) {
        $stylesheet = func_get_arg(1);
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    $template = $new_theme->get_template();
    if (wp_is_recovery_mode()) {
        $paused_themes = wp_paused_themes();
        $paused_themes->delete($old_theme->get_stylesheet());
        $paused_themes->delete($old_theme->get_template());
    }
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        if (!empty($nav_menu_locations) && empty($default_theme_mods['nav_menu_locations'])) {
            $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
        }
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else if ('wp_ajax_customize_save' === current_action()) {
        remove_theme_mod('sidebars_widgets');
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /**
     * Fires after the theme is switched.
     *
     * @since 1.5.0
     * @since 4.5.0 Introduced the `$old_theme` parameter.
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     * @param WP_Theme $old_theme WP_Theme instance of the old theme.
     */
    do_action('switch_theme', $new_name, $new_theme, $old_theme);
}

WordPress Version: 5.2

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backward compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 *
 * @param string $stylesheet Stylesheet name
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets;
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $old_sidebars_widgets_data_setting = $wp_customize->get_setting('old_sidebars_widgets_data');
        if ($old_sidebars_widgets_data_setting) {
            $_sidebars_widgets = $wp_customize->post_value($old_sidebars_widgets_data_setting);
        }
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $nav_menu_locations = get_theme_mod('nav_menu_locations');
    update_option('theme_switch_menu_locations', $nav_menu_locations);
    if (func_num_args() > 1) {
        $stylesheet = func_get_arg(1);
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    $template = $new_theme->get_template();
    if (wp_is_recovery_mode()) {
        $paused_themes = wp_paused_themes();
        $paused_themes->delete($old_theme->get_stylesheet());
        $paused_themes->delete($old_theme->get_template());
    }
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        if (!empty($nav_menu_locations) && empty($default_theme_mods['nav_menu_locations'])) {
            $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
        }
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else if ('wp_ajax_customize_save' === current_action()) {
        remove_theme_mod('sidebars_widgets');
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /**
     * Fires after the theme is switched.
     *
     * @since 1.5.0
     * @since 4.5.0 Introduced the `$old_theme` parameter.
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     * @param WP_Theme $old_theme WP_Theme instance of the old theme.
     */
    do_action('switch_theme', $new_name, $new_theme, $old_theme);
}

WordPress Version: 4.9

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backward compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 *
 * @param string $stylesheet Stylesheet name
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets;
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $old_sidebars_widgets_data_setting = $wp_customize->get_setting('old_sidebars_widgets_data');
        if ($old_sidebars_widgets_data_setting) {
            $_sidebars_widgets = $wp_customize->post_value($old_sidebars_widgets_data_setting);
        }
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $nav_menu_locations = get_theme_mod('nav_menu_locations');
    update_option('theme_switch_menu_locations', $nav_menu_locations);
    if (func_num_args() > 1) {
        $stylesheet = func_get_arg(1);
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    $template = $new_theme->get_template();
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        if (!empty($nav_menu_locations) && empty($default_theme_mods['nav_menu_locations'])) {
            $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
        }
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else if ('wp_ajax_customize_save' === current_action()) {
        remove_theme_mod('sidebars_widgets');
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /**
     * Fires after the theme is switched.
     *
     * @since 1.5.0
     * @since 4.5.0 Introduced the `$old_theme` parameter.
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     * @param WP_Theme $old_theme WP_Theme instance of the old theme.
     */
    do_action('switch_theme', $new_name, $new_theme, $old_theme);
}

WordPress Version: 4.6

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backward compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 *
 * @param string $stylesheet Stylesheet name
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets;
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $_sidebars_widgets = $wp_customize->post_value($wp_customize->get_setting('old_sidebars_widgets_data'));
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $nav_menu_locations = get_theme_mod('nav_menu_locations');
    if (func_num_args() > 1) {
        $stylesheet = func_get_arg(1);
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    $template = $new_theme->get_template();
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        if (!empty($nav_menu_locations) && empty($default_theme_mods['nav_menu_locations'])) {
            $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
        }
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else {
        /*
         * Since retrieve_widgets() is called when initializing a theme in the Customizer,
         * we need to remove the theme mods to avoid overwriting changes made via
         * the Customizer when accessing wp-admin/widgets.php.
         */
        if ('wp_ajax_customize_save' === current_action()) {
            remove_theme_mod('sidebars_widgets');
        }
        if (!empty($nav_menu_locations)) {
            $nav_mods = get_theme_mod('nav_menu_locations');
            if (empty($nav_mods)) {
                set_theme_mod('nav_menu_locations', $nav_menu_locations);
            }
        }
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /**
     * Fires after the theme is switched.
     *
     * @since 1.5.0
     * @since 4.5.0 Introduced the `$old_theme` parameter.
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     * @param WP_Theme $old_theme WP_Theme instance of the old theme.
     */
    do_action('switch_theme', $new_name, $new_theme, $old_theme);
}

WordPress Version: 4.5

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backwards compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 *
 * @param string $stylesheet Stylesheet name
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets;
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $_sidebars_widgets = $wp_customize->post_value($wp_customize->get_setting('old_sidebars_widgets_data'));
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $nav_menu_locations = get_theme_mod('nav_menu_locations');
    if (func_num_args() > 1) {
        $stylesheet = func_get_arg(1);
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    $template = $new_theme->get_template();
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        if (!empty($nav_menu_locations) && empty($default_theme_mods['nav_menu_locations'])) {
            $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
        }
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else {
        /*
         * Since retrieve_widgets() is called when initializing a theme in the Customizer,
         * we need to remove the theme mods to avoid overwriting changes made via
         * the Customizer when accessing wp-admin/widgets.php.
         */
        if ('wp_ajax_customize_save' === current_action()) {
            remove_theme_mod('sidebars_widgets');
        }
        if (!empty($nav_menu_locations)) {
            $nav_mods = get_theme_mod('nav_menu_locations');
            if (empty($nav_mods)) {
                set_theme_mod('nav_menu_locations', $nav_menu_locations);
            }
        }
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /**
     * Fires after the theme is switched.
     *
     * @since 1.5.0
     * @since 4.5.0 Introduced the `$old_theme` parameter.
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     * @param WP_Theme $old_theme WP_Theme instance of the old theme.
     */
    do_action('switch_theme', $new_name, $new_theme, $old_theme);
}

WordPress Version: 4.4

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backwards compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 *
 * @param string $stylesheet Stylesheet name
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets;
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $_sidebars_widgets = $wp_customize->post_value($wp_customize->get_setting('old_sidebars_widgets_data'));
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $nav_menu_locations = get_theme_mod('nav_menu_locations');
    if (func_num_args() > 1) {
        $stylesheet = func_get_arg(1);
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    $template = $new_theme->get_template();
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        if (!empty($nav_menu_locations) && empty($default_theme_mods['nav_menu_locations'])) {
            $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
        }
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else {
        /*
         * Since retrieve_widgets() is called when initializing a theme in the Customizer,
         * we need to to remove the theme mods to avoid overwriting changes made via
         * the Customizer when accessing wp-admin/widgets.php.
         */
        if ('wp_ajax_customize_save' === current_action()) {
            remove_theme_mod('sidebars_widgets');
        }
        if (!empty($nav_menu_locations)) {
            $nav_mods = get_theme_mod('nav_menu_locations');
            if (empty($nav_mods)) {
                set_theme_mod('nav_menu_locations', $nav_menu_locations);
            }
        }
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /**
     * Fires after the theme is switched.
     *
     * @since 1.5.0
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     */
    do_action('switch_theme', $new_name, $new_theme);
}

WordPress Version: 4.3

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backwards compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 *
 * @param string $stylesheet Stylesheet name
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets;
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $_sidebars_widgets = $wp_customize->post_value($wp_customize->get_setting('old_sidebars_widgets_data'));
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    if (func_num_args() > 1) {
        $template = $stylesheet;
        $stylesheet = func_get_arg(1);
    } else {
        $template = $new_theme->get_template();
    }
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else if ('wp_ajax_customize_save' === current_action()) {
        remove_theme_mod('sidebars_widgets');
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /**
     * Fires after the theme is switched.
     *
     * @since 1.5.0
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     */
    do_action('switch_theme', $new_name, $new_theme);
}

WordPress Version: 3.9

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backwards compatibility.
 *
 * @since 2.5.0
 *
 * @param string $stylesheet Stylesheet name
 */
function switch_theme($stylesheet)
{
    global $wp_theme_directories, $wp_customize, $sidebars_widgets;
    $_sidebars_widgets = null;
    if ('wp_ajax_customize_save' === current_action()) {
        $_sidebars_widgets = $wp_customize->post_value($wp_customize->get_setting('old_sidebars_widgets_data'));
    } elseif (is_array($sidebars_widgets)) {
        $_sidebars_widgets = $sidebars_widgets;
    }
    if (is_array($_sidebars_widgets)) {
        set_theme_mod('sidebars_widgets', array('time' => time(), 'data' => $_sidebars_widgets));
    }
    $old_theme = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    if (func_num_args() > 1) {
        $template = $stylesheet;
        $stylesheet = func_get_arg(1);
    } else {
        $template = $new_theme->get_template();
    }
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    // Migrate from the old mods_{name} option to theme_mods_{slug}.
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    } else if ('wp_ajax_customize_save' === current_action()) {
        remove_theme_mod('sidebars_widgets');
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /**
     * Fires after the theme is switched.
     *
     * @since 1.5.0
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     */
    do_action('switch_theme', $new_name, $new_theme);
}

WordPress Version: 3.8

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backwards compatibility.
 *
 * @since 2.5.0
 *
 * @param string $stylesheet Stylesheet name
 */
function switch_theme($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 = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    if (func_num_args() > 1) {
        $template = $stylesheet;
        $stylesheet = func_get_arg(1);
    } else {
        $template = $new_theme->get_template();
    }
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    /**
     * Fires after the theme is switched.
     *
     * @since 1.5.0
     *
     * @param string   $new_name  Name of the new theme.
     * @param WP_Theme $new_theme WP_Theme instance of the new theme.
     */
    do_action('switch_theme', $new_name, $new_theme);
}

WordPress Version: 3.7

/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backwards compatibility.
 *
 * @since 2.5.0
 * @uses do_action() Calls 'switch_theme' action, passing the new theme.
 *
 * @param string $stylesheet Stylesheet name
 */
function switch_theme($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 = wp_get_theme();
    $new_theme = wp_get_theme($stylesheet);
    if (func_num_args() > 1) {
        $template = $stylesheet;
        $stylesheet = func_get_arg(1);
    } else {
        $template = $new_theme->get_template();
    }
    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));
    } else {
        delete_option('template_root');
        delete_option('stylesheet_root');
    }
    $new_name = $new_theme->get('Name');
    update_option('current_theme', $new_name);
    if (is_admin() && false === get_option('theme_mods_' . $stylesheet)) {
        $default_theme_mods = (array) get_option('mods_' . $new_name);
        add_option("theme_mods_{$stylesheet}", $default_theme_mods);
    }
    update_option('theme_switched', $old_theme->get_stylesheet());
    do_action('switch_theme', $new_name, $new_theme);
}