wppaste
WordPress

check_theme_switched()

Since
3.3.0
Source
wp-includes/theme.php:3475
Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load.

Description

See 'after_switch_theme'.

Performance profile

How much work a call to check_theme_switched() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6–44

Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 49.

Plugin surface
1 hook

Third-party callbacks on 'after_switch_theme' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksdo_action()called directly
  • cacheobject cachewp_cache_get()one call below check_theme_switched()
  • serializeserialisationmaybe_unserialize()one call below check_theme_switched()

Further down the call graph this can also reach transient and query those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 5 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost check_theme_switched() can have, taken from its control-flow graph on PHP 8.4.

WhenInstructionsCalls it makes
always6get_option()
!get_option() && !->exists()28get_option()wp_get_theme()get_option()->exists()do_action()flush_rewrite_rules()update_option()
!get_option() && ->exists()32get_option()wp_get_theme()get_option()->exists()->get()do_action()flush_rewrite_rules()update_option()
get_option() && !->exists()40get_option()wp_get_theme()get_option()remove_action()remove_action()update_option()->exists()do_action()flush_rewrite_rules()update_option()
get_option() && ->exists()44get_option()wp_get_theme()get_option()remove_action()remove_action()update_option()->exists()->get()do_action()flush_rewrite_rules()update_option()

Across PHP versions

PHPCompiledExecutedBranchesNotes
7.4496–443Compiles to the same instructions
8.1496–443Compiles to the same instructions
8.2496–443Compiles to the same instructions
8.3496–443Compiles to the same instructions
8.4496–443Compiles to the same instructions

Oldest PHP that compiles this body: 7.4. An instruction is not a fixed amount of time, so a version with the same count is not necessarily the same speed; what the count rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while check_theme_switched() runs, in this order:

  1. do_action( after_switch_theme )actionline 3503 (+28 into the body)

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

  2. do_action( after_switch_theme )actionline 3506 (+31 into the body)

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

Uses · 6

Source code

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 {@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 );	}}

Changelog

Introduced in 3.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/theme.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.