check_theme_switched

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

WordPress Version: 6.3

/**
 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load.
 *
 * See {@see 'after_switch_theme'}.
 *
 * @since 3.3.0
 */
function check_theme_switched()
{
    $stylesheet = get_option('theme_switched');
    if ($stylesheet) {
        $old_theme = wp_get_theme($stylesheet);
        // Prevent widget & menu mapping from running since Customizer already called it up front.
        if (get_option('theme_switched_via_customizer')) {
            remove_action('after_switch_theme', '_wp_menus_changed');
            remove_action('after_switch_theme', '_wp_sidebars_changed');
            update_option('theme_switched_via_customizer', false);
        }
        if ($old_theme->exists()) {
            /**
             * Fires on the next WP load after the theme has been switched.
             *
             * The parameters differ according to whether the old theme exists or not.
             * If the old theme is missing, the old name will instead be the slug
             * of the old theme.
             *
             * See {@see 'switch_theme'}.
             *
             * @since 3.3.0
             *
             * @param string   $old_name  Old theme name.
             * @param WP_Theme $old_theme WP_Theme instance of the old theme.
             */
            do_action('after_switch_theme', $old_theme->get('Name'), $old_theme);
        } else {
            /** This action is documented in wp-includes/theme.php */
            do_action('after_switch_theme', $stylesheet, $old_theme);
        }
        flush_rewrite_rules();
        update_option('theme_switched', false);
    }
}

WordPress Version: 5.4

/**
 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load.
 *
 * See {@see 'after_switch_theme'}.
 *
 * @since 3.3.0
 */
function check_theme_switched()
{
    $stylesheet = get_option('theme_switched');
    if ($stylesheet) {
        $old_theme = wp_get_theme($stylesheet);
        // Prevent widget & menu mapping from running since Customizer already called it up front.
        if (get_option('theme_switched_via_customizer')) {
            remove_action('after_switch_theme', '_wp_menus_changed');
            remove_action('after_switch_theme', '_wp_sidebars_changed');
            update_option('theme_switched_via_customizer', false);
        }
        if ($old_theme->exists()) {
            /**
             * Fires on the first WP load after a theme switch if the old theme still exists.
             *
             * This action fires multiple times and the parameters differs
             * according to the context, if the old theme exists or not.
             * If the old theme is missing, the parameter will be the slug
             * of the old theme.
             *
             * @since 3.3.0
             *
             * @param string   $old_name  Old theme name.
             * @param WP_Theme $old_theme WP_Theme instance of the old theme.
             */
            do_action('after_switch_theme', $old_theme->get('Name'), $old_theme);
        } else {
            /** This action is documented in wp-includes/theme.php */
            do_action('after_switch_theme', $stylesheet, $old_theme);
        }
        flush_rewrite_rules();
        update_option('theme_switched', false);
    }
}

WordPress Version: 5.3

/**
 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load.
 *
 * See {@see 'after_switch_theme'}.
 *
 * @since 3.3.0
 */
function check_theme_switched()
{
    $stylesheet = get_option('theme_switched');
    if ($stylesheet) {
        $old_theme = wp_get_theme($stylesheet);
        // Prevent widget & menu mapping from running since Customizer already called it up front
        if (get_option('theme_switched_via_customizer')) {
            remove_action('after_switch_theme', '_wp_menus_changed');
            remove_action('after_switch_theme', '_wp_sidebars_changed');
            update_option('theme_switched_via_customizer', false);
        }
        if ($old_theme->exists()) {
            /**
             * Fires on the first WP load after a theme switch if the old theme still exists.
             *
             * This action fires multiple times and the parameters differs
             * according to the context, if the old theme exists or not.
             * If the old theme is missing, the parameter will be the slug
             * of the old theme.
             *
             * @since 3.3.0
             *
             * @param string   $old_name  Old theme name.
             * @param WP_Theme $old_theme WP_Theme instance of the old theme.
             */
            do_action('after_switch_theme', $old_theme->get('Name'), $old_theme);
        } else {
            /** This action is documented in wp-includes/theme.php */
            do_action('after_switch_theme', $stylesheet, $old_theme);
        }
        flush_rewrite_rules();
        update_option('theme_switched', false);
    }
}

WordPress Version: 4.9

/**
 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load.
 *
 * See {@see 'after_switch_theme'}.
 *
 * @since 3.3.0
 */
function check_theme_switched()
{
    if ($stylesheet = get_option('theme_switched')) {
        $old_theme = wp_get_theme($stylesheet);
        // Prevent widget & menu mapping from running since Customizer already called it up front
        if (get_option('theme_switched_via_customizer')) {
            remove_action('after_switch_theme', '_wp_menus_changed');
            remove_action('after_switch_theme', '_wp_sidebars_changed');
            update_option('theme_switched_via_customizer', false);
        }
        if ($old_theme->exists()) {
            /**
             * Fires on the first WP load after a theme switch if the old theme still exists.
             *
             * This action fires multiple times and the parameters differs
             * according to the context, if the old theme exists or not.
             * If the old theme is missing, the parameter will be the slug
             * of the old theme.
             *
             * @since 3.3.0
             *
             * @param string   $old_name  Old theme name.
             * @param WP_Theme $old_theme WP_Theme instance of the old theme.
             */
            do_action('after_switch_theme', $old_theme->get('Name'), $old_theme);
        } else {
            /** This action is documented in wp-includes/theme.php */
            do_action('after_switch_theme', $stylesheet, $old_theme);
        }
        flush_rewrite_rules();
        update_option('theme_switched', false);
    }
}

WordPress Version: 4.6

/**
 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load.
 *
 * See {@see 'after_switch_theme'}.
 *
 * @since 3.3.0
 */
function check_theme_switched()
{
    if ($stylesheet = get_option('theme_switched')) {
        $old_theme = wp_get_theme($stylesheet);
        // Prevent retrieve_widgets() from running since Customizer already called it up front
        if (get_option('theme_switched_via_customizer')) {
            remove_action('after_switch_theme', '_wp_sidebars_changed');
            update_option('theme_switched_via_customizer', false);
        }
        if ($old_theme->exists()) {
            /**
             * Fires on the first WP load after a theme switch if the old theme still exists.
             *
             * This action fires multiple times and the parameters differs
             * according to the context, if the old theme exists or not.
             * If the old theme is missing, the parameter will be the slug
             * of the old theme.
             *
             * @since 3.3.0
             *
             * @param string   $old_name  Old theme name.
             * @param WP_Theme $old_theme WP_Theme instance of the old theme.
             */
            do_action('after_switch_theme', $old_theme->get('Name'), $old_theme);
        } else {
            /** This action is documented in wp-includes/theme.php */
            do_action('after_switch_theme', $stylesheet);
        }
        flush_rewrite_rules();
        update_option('theme_switched', false);
    }
}

WordPress Version: 4.4

/**
 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
 *
 * @since 3.3.0
 */
function check_theme_switched()
{
    if ($stylesheet = get_option('theme_switched')) {
        $old_theme = wp_get_theme($stylesheet);
        // Prevent retrieve_widgets() from running since Customizer already called it up front
        if (get_option('theme_switched_via_customizer')) {
            remove_action('after_switch_theme', '_wp_sidebars_changed');
            update_option('theme_switched_via_customizer', false);
        }
        if ($old_theme->exists()) {
            /**
             * Fires on the first WP load after a theme switch if the old theme still exists.
             *
             * This action fires multiple times and the parameters differs
             * according to the context, if the old theme exists or not.
             * If the old theme is missing, the parameter will be the slug
             * of the old theme.
             *
             * @since 3.3.0
             *
             * @param string   $old_name  Old theme name.
             * @param WP_Theme $old_theme WP_Theme instance of the old theme.
             */
            do_action('after_switch_theme', $old_theme->get('Name'), $old_theme);
        } else {
            /** This action is documented in wp-includes/theme.php */
            do_action('after_switch_theme', $stylesheet);
        }
        flush_rewrite_rules();
        update_option('theme_switched', false);
    }
}

WordPress Version: 3.9

/**
 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
 *
 * @since 3.3.0
 */
function check_theme_switched()
{
    if ($stylesheet = get_option('theme_switched')) {
        $old_theme = wp_get_theme($stylesheet);
        // Prevent retrieve_widgets() from running since Customizer already called it up front
        if (get_option('theme_switched_via_customizer')) {
            remove_action('after_switch_theme', '_wp_sidebars_changed');
            update_option('theme_switched_via_customizer', false);
        }
        if ($old_theme->exists()) {
            /**
             * Fires on the first WP load after a theme switch if the old theme still exists.
             *
             * This action fires multiple times and the parameters differs
             * according to the context, if the old theme exists or not.
             * If the old theme is missing, the parameter will be the slug
             * of the old theme.
             *
             * @since 3.3.0
             *
             * @param string   $old_name  Old theme name.
             * @param WP_Theme $old_theme WP_Theme instance of the old theme.
             */
            do_action('after_switch_theme', $old_theme->get('Name'), $old_theme);
        } else {
            /** This action is documented in wp-includes/theme.php */
            do_action('after_switch_theme', $stylesheet);
        }
        update_option('theme_switched', false);
    }
}

WordPress Version: 3.8

/**
 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
 *
 * @since 3.3.0
 */
function check_theme_switched()
{
    if ($stylesheet = get_option('theme_switched')) {
        $old_theme = wp_get_theme($stylesheet);
        if ($old_theme->exists()) {
            /**
             * Fires on the first WP load after a theme switch if the old theme still exists.
             *
             * This action fires multiple times and the parameters differs
             * according to the context, if the old theme exists or not.
             * If the old theme is missing, the parameter will be the slug
             * of the old theme.
             *
             * @since 3.3.0
             *
             * @param string   $old_name  Old theme name.
             * @param WP_Theme $old_theme WP_Theme instance of the old theme.
             */
            do_action('after_switch_theme', $old_theme->get('Name'), $old_theme);
        } else {
            /** This action is documented in wp-includes/theme.php */
            do_action('after_switch_theme', $stylesheet);
        }
        update_option('theme_switched', false);
    }
}

WordPress Version: 3.7

/**
 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
 *
 * @since 3.3.0
 */
function check_theme_switched()
{
    if ($stylesheet = get_option('theme_switched')) {
        $old_theme = wp_get_theme($stylesheet);
        if ($old_theme->exists()) {
            do_action('after_switch_theme', $old_theme->get('Name'), $old_theme);
        } else {
            do_action('after_switch_theme', $stylesheet);
        }
        update_option('theme_switched', false);
    }
}