check_theme_switched()
- Since
- 3.3.0
- Source
wp-includes/theme.php:3476
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
- Scaling
- Constant
- Instructions
- 6–44
- Plugin surface
- 1 hook
- Called by
- 0
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 49.
Third-party callbacks on 'after_switch_theme' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
do_action()called directly - cacheobject cache
wp_cache_get()one call below check_theme_switched() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | get_option() |
!get_option() && !->exists() | 28 | get_option(), wp_get_theme(), get_option(), ->exists(), do_action(), flush_rewrite_rules(), update_option() |
!get_option() && ->exists() | 32 | get_option(), wp_get_theme(), get_option(), ->exists(), ->get(), do_action(), flush_rewrite_rules(), update_option() |
get_option() && !->exists() | 40 | get_option(), wp_get_theme(), get_option(), remove_action(), remove_action(), update_option(), ->exists(), do_action(), flush_rewrite_rules(), update_option() |
get_option() && ->exists() | 44 | get_option(), wp_get_theme(), get_option(), remove_action(), remove_action(), update_option(), ->exists(), ->get(), do_action(), flush_rewrite_rules(), update_option() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3 and 8.4: 49 instructions, 6–44 executed per call, 3 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it 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:
- do_action( after_switch_theme )actionline 3504 (+28 into the body)
Fires on the next WP load after the theme has been switched.
- do_action( after_switch_theme )actionline 3507 (+31 into the body)
Fires on the next WP load after the theme has been switched.
Uses · 6
- get_option()Retrieves an option value based on an option name.
- wp_get_theme()Gets a WP_Theme object for a theme.
- remove_action()Removes a callback function from an action hook.
- update_option()Updates the value of an option that was already added.
- do_action()Calls the callback functions that have been added to an action hook.
- flush_rewrite_rules()Removes rewrite rules and then recreate rewrite rules.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.