Title: check_theme_switched
Published: April 25, 2014
Last modified: February 24, 2026

---

# check_theme_switched()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#description)
 * [Source](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#wp--skip-link--target)

Checks if a theme has been changed and runs ‘after_switch_theme’ hook on the next
WP load.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#description)󠁿

See [‘after_switch_theme’](https://developer.wordpress.org/reference/hooks/after_switch_theme/).

## 󠀁[Source](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#source)󠁿

    ```php
    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 '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 );
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/theme.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/theme.php#L3488)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/theme.php#L3488-L3526)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#hooks)󠁿

 [do_action( ‘after_switch_theme’, string $old_name, WP_Theme $old_theme )](https://developer.wordpress.org/reference/hooks/after_switch_theme/)

Fires on the next WP load after the theme has been switched.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#related)󠁿

| Uses | Description | 
| [remove_action()](https://developer.wordpress.org/reference/functions/remove_action/)`wp-includes/plugin.php` |

Removes a callback function from an action hook.

  | 
| [flush_rewrite_rules()](https://developer.wordpress.org/reference/functions/flush_rewrite_rules/)`wp-includes/rewrite.php` |

Removes rewrite rules and then recreate rewrite rules.

  | 
| [wp_get_theme()](https://developer.wordpress.org/reference/functions/wp_get_theme/)`wp-includes/theme.php` |

Gets a [WP_Theme](https://developer.wordpress.org/reference/classes/wp_theme/) object for a theme.

  | 
| [do_action()](https://developer.wordpress.org/reference/functions/do_action/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to an action hook.

  | 
| [update_option()](https://developer.wordpress.org/reference/functions/update_option/)`wp-includes/option.php` |

Updates the value of an option that was already added.

  | 
| [get_option()](https://developer.wordpress.org/reference/functions/get_option/)`wp-includes/option.php` |

Retrieves an option value based on an option name.

  |

[Show 4 more](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/check_theme_switched/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.3.0](https://developer.wordpress.org/reference/since/3.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fcheck_theme_switched%2F)
before being able to contribute a note or feedback.