Make WordPress Core

Opened 7 years ago

Last modified 5 years ago

#40221 new defect (bug)

switch_theme action + Live Preview = confusion

Reported by: alexvorn2's profile alexvorn2 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.3
Component: Themes Keywords: needs-patch
Focuses: Cc:


The switch_theme function has inside a hook named the same: switch_theme, it is used for theme deactivation actions, but the problem is that after you activate the theme via Live Preview - this action is executed, so can create confusion.

(switch_theme hook is for theme deactivation functions -

When we activate a theme (not via Live Preview) - then this hook is executed together with old theme, not with new theme, but when we have a Live Preview with a new theme, then this hook will be used with the theme that is inside the Live Preview, with it's files. That's why this hook is called incorrectly.

So if this is not by design and it is a bug then the fix would be to skip this hook after a theme activation via Live Preview.

Change History (2)

#1 @alexvorn2
7 years ago

I tested and noticed that skipping the hook, after theme activation via Live Preview - will not solve the problem, we still need this hook to be executed on old theme somehow.
Old theme can have some deactivation functions that are needed to be executed.

#2 @joyously
5 years ago

  • Keywords needs-patch added

The switch_theme action is for when the theme is switched, and is supposed to run in the context of the old theme. It is passed the new and the old theme object.
Searching for it in the WP 5.1 code, I found these:
wp-includes\class-wp-customize-manager.php(2840): switch_theme( $this->get_stylesheet() );
wp-includes\class-wp-customize-manager.php(3463): if ( did_action( 'switch_theme' ) ) {
wp-includes\theme.php(713): function switch_theme( $stylesheet ) {
wp-includes\theme.php(792): do_action( 'switch_theme', $new_name, $new_theme, $old_theme );
wp-includes\theme.php(835): switch_theme( WP_DEFAULT_THEME );
wp-includes\theme.php(853): switch_theme( $default->get_stylesheet() );
wp-admin\themes.php(33): switch_theme( $theme->get_stylesheet() );
wp-admin\includes\class-theme-upgrader.php(577): switch_theme( $stylesheet );

The code appears as if it is always called in the context of the old theme, but in the case of Live Preview it is the theme being previewed (new theme) and not the theme on the front end (old theme), since that is what is already loaded for the preview. The action's parameters are correct for the old and new Theme objects, however.

Note: See TracTickets for help on using tickets.