diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index f063bc97a0f84..81c91ab5cc8a5 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -840,6 +840,13 @@ function switch_theme( $stylesheet ) { $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. * diff --git a/tests/phpunit/tests/theme/autoloadThemeMods.php b/tests/phpunit/tests/theme/autoloadThemeMods.php new file mode 100644 index 0000000000000..e653ac6a77a84 --- /dev/null +++ b/tests/phpunit/tests/theme/autoloadThemeMods.php @@ -0,0 +1,42 @@ +assertSame( 'no', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", "theme_mods_$current_theme_stylesheet" ) ), 'Theme mods autoload value not set to no in database' ); + $this->assertSame( 'yes', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", "theme_mods_$new_theme_stylesheet" ) ), 'Theme mods autoload value not set to yes in database' ); + + switch_theme( $current_theme_stylesheet ); + + $this->assertSame( 'yes', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", "theme_mods_$current_theme_stylesheet" ) ), 'Theme mods autoload value not set to yes in database' ); + $this->assertSame( 'no', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", "theme_mods_$new_theme_stylesheet" ) ), 'Theme mods autoload value not set to no in database' ); + + // Basic assertion to make sure that we haven't lost the mods. + $this->assertSame( 'a-value', get_theme_mod( 'foo-bar-option' ) ); + } +}