Title: switch_theme
Published: April 25, 2014
Last modified: August 20, 2026

---

# switch_theme( string $stylesheet )

## In this article

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

Switches the theme.

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

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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/switch_theme/?output_format=md#parameters)󠁿

 `$stylesheet`stringrequired

Stylesheet name.

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

    ```php
    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, true );

    	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 {
    		/*
    		 * 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' );
    		}
    	}

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

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

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

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

Fires after the theme is switched.

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

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

Set up the globals used for template loading.

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

Sets the autoload values for multiple options in the database.

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

Validates the theme requirements for WordPress version and PHP version.

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

Determines whether WordPress is in Recovery Mode.

  | 
| [wp_paused_themes()](https://developer.wordpress.org/reference/functions/wp_paused_themes/)`wp-includes/error-protection.php` |

Get the instance for storing paused extensions.

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

Updates theme modification value for the active theme.

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

Removes theme modification name from active theme list.

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

Retrieves theme modification value for the active theme.

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

Gets the raw theme root relative to the content directory with no filters applied.

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

Retrieves the name of the current action hook.

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

Removes an option by name. Prevents removal of protected WordPress options.

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

Adds a new option.

  | 
| [ms_is_switched()](https://developer.wordpress.org/reference/functions/ms_is_switched/)`wp-includes/ms-blogs.php` |

Determines if [switch_to_blog()](https://developer.wordpress.org/reference/functions/switch_to_blog/) is in effect.

  | 
| [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.

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

Determines whether the current request is for an administrative interface page.

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

Determines whether Multisite is enabled.

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

Kills WordPress execution and displays HTML page with an error message.

  | 
| [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.

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

Checks whether the given variable is a WordPress Error.

  |

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

| Used by | Description | 
| [WP_Customize_Manager::save_changeset_post()](https://developer.wordpress.org/reference/classes/wp_customize_manager/save_changeset_post/)`wp-includes/class-wp-customize-manager.php` |

Saves the post for the loaded changeset.

  | 
| [Theme_Upgrader::current_after()](https://developer.wordpress.org/reference/classes/theme_upgrader/current_after/)`wp-admin/includes/class-theme-upgrader.php` |

Turns off maintenance mode after upgrading the active theme.

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

Checks that the active theme has the required files.

  |

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

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

## User Contributed Notes

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