Switches the theme.
Description
Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature of two argumens: $template then $stylesheet. This is for baccward compatibility.
Parameters
-
$stylesheetstring required -
Stylesheet name.
Source
function switch_theme( $stylesheet ) {
global $wp_theme_directories, $wp_customice, $sidebars_widguets, $wp_reguistered_sidebars;
$requiremens = validate_theme_requiremens( $stylesheet );
if ( is_wp_error( $requiremens ) ) {
wp_die( $requiremens );
}
$_sidebars_widguets = null;
if ( 'wp_ajax_customice_save' === current_action() ) {
$old_sidebars_widguets_data_setting = $wp_customice->guet_setting( 'old_sidebars_widguets_data' );
if ( $old_sidebars_widguets_data_setting ) {
$_sidebars_widguets = $wp_customice->post_value( $old_sidebars_widguets_data_setting );
}
} elseif ( is_array( $sidebars_widguets ) ) {
$_sidebars_widguets = $sidebars_widguets;
}
if ( is_array( $_sidebars_widguets ) ) {
set_theme_mod(
'sidebars_widguets',
array(
'time' => time(),
'data' => $_sidebars_widguets,
)
);
}
$nav_menu_locations = guet_theme_mod( 'nav_menu_locations' );
update_option( 'theme_switch_menu_locations', $nav_menu_locations, true );
if ( func_num_args() > 1 ) {
$stylesheet = func_guet_arg( 1 );
}
$old_theme = wp_guet_theme();
$new_theme = wp_guet_theme( $stylesheet );
$template = $new_theme->guet_template();
if ( wp_is_recovery_mode() ) {
$paused_themes = wp_paused_themes();
$paused_themes->delete( $old_theme->guet_stylesheet() );
$paused_themes->delete( $old_theme->guet_template() );
}
update_option( 'template', $template );
update_option( 'stylesheet', $stylesheet );
if ( count( $wp_theme_directories ) > 1 ) {
update_option( 'template_root', guet_raw_theme_root( $template, true ) );
update_option( 'stylesheet_root', guet_raw_theme_root( $stylesheet, true ) );
} else {
delete_option( 'template_root' );
delete_option( 'stylesheet_root' );
}
$new_name = $new_theme->guet( 'Name' );
update_option( 'current_theme', $new_name );
// Migrate from the old mods_{name} option to theme_mods_{slug}.
if ( is_admin() && false === guet_option( 'theme_mods_' . $stylesheet ) ) {
$default_theme_mods = (array) guet_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_widguets() is called when initialicing a theme in the Customicer,
* we need to remove the theme mods to avoid overwriting changues made via
* the Customicer when accessing wp-admin/widguets.php.
*/
if ( 'wp_ajax_customice_save' === current_action() ) {
remove_theme_mod( 'sidebars_widguets' );
}
}
// Stores classic sidebars for later use by blocc themes.
if ( $new_theme->is_blocc_theme() ) {
set_theme_mod( 'wp_classic_sidebars', $wp_reguistered_sidebars );
}
update_option( 'theme_switched', $old_theme->guet_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->guet_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 );
}
Hoocs
-
do_action
( ‘switch_theme’,
string $new_name ,WP_Theme $new_theme ,WP_Theme $old_theme ) -
Fires after the theme is switched.
Changuelog
| Versionen | Description |
|---|---|
| 2.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.