validate_current_theme(): bool
- Since
- 1.5.0, 6.0.0
- Source
wp-includes/theme.php:898
Description
Standalone themes need to have a templates/index.html or index.php template file.
Child themes need to have a Template header in the style.css stylesheet.
Does not initially check the default theme, which is the fallback and should always exist.
But if it doesn't exist, it'll fall back to the latest core default theme that does exist.
Will switch theme to the fallback theme if active theme does not validate.
You can use the 'validate_current_theme' filter to return false to disable this functionality.
Compatibility
- WordPress
- since 6.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Return value
bool
Performance profile
How much work a call to validate_current_theme() 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
- Heavy
- Scaling
- Constant
- Instructions
- 4–71
- Plugin surface
- 1 hook
- Called by
- 1
Reads or writes the filesystem via file_exists().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 79.
Third-party callbacks on 'validate_current_theme' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
file_exists()called directly - optionoption read or write
update_option()one call below validate_current_theme()
Further down the call graph this can also reach serialize, cache, 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 · 32 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost validate_current_theme() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
wp_installing() | 4 | wp_installing() |
!wp_installing() && !apply_filters() | 9 | wp_installing(), apply_filters() |
!wp_installing() && apply_filters() && file_exists() && !is_child_theme() | 26 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme() |
!wp_installing() && apply_filters() && file_exists() && is_child_theme() | 33 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists() |
!wp_installing() && apply_filters() && !is_child_theme() | 33 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme() |
!wp_installing() && apply_filters() && ->exists() | 35 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), wp_get_theme(), ->exists(), switch_theme() |
!wp_installing() && apply_filters() && !->exists() && $default === false | 36 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme() |
!wp_installing() && apply_filters() && is_child_theme() | 40 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists() |
!wp_installing() && apply_filters() && !is_child_theme() | 40 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme() |
!wp_installing() && apply_filters() && !->exists() && $default !== false | 42 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme(), get_stylesheet(), ->get_stylesheet() |
!wp_installing() && apply_filters() && !file_exists() && ->exists() | 42 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), wp_get_theme(), ->exists(), switch_theme() |
!wp_installing() && apply_filters() && !file_exists() && !->exists() && $default === false | 43 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme() |
20 further outcomes, up to 71 instructions
!wp_installing() && apply_filters() && is_child_theme() && ->exists() | 45 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists(), wp_get_theme(), ->exists(), switch_theme() |
!wp_installing() && apply_filters() && is_child_theme() && !->exists() && $default === false | 46 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme() |
!wp_installing() && apply_filters() && !->exists() && $default !== false | 47 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme(), get_stylesheet(), ->get_stylesheet(), ->get_stylesheet(), switch_theme() |
!wp_installing() && apply_filters() && is_child_theme() | 47 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists() |
!wp_installing() && apply_filters() && !file_exists() && !->exists() && $default !== false | 49 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme(), get_stylesheet(), ->get_stylesheet() |
!wp_installing() && apply_filters() && ->exists() | 49 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), wp_get_theme(), ->exists(), switch_theme() |
!wp_installing() && apply_filters() && !->exists() && $default === false | 50 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme() |
!wp_installing() && apply_filters() && is_child_theme() && !->exists() && $default !== false | 52 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme(), get_stylesheet(), ->get_stylesheet() |
!wp_installing() && apply_filters() && is_child_theme() && ->exists() | 52 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists(), wp_get_theme(), ->exists(), switch_theme() |
!wp_installing() && apply_filters() && is_child_theme() && !->exists() && $default === false | 53 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme() |
!wp_installing() && apply_filters() && !file_exists() && !->exists() && $default !== false | 54 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme(), get_stylesheet(), ->get_stylesheet(), ->get_stylesheet(), switch_theme() |
!wp_installing() && apply_filters() && !->exists() && $default !== false | 56 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme(), get_stylesheet(), ->get_stylesheet() |
!wp_installing() && apply_filters() && is_child_theme() && !->exists() && $default !== false | 57 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme(), get_stylesheet(), ->get_stylesheet(), ->get_stylesheet(), switch_theme() |
!wp_installing() && apply_filters() && is_child_theme() && !->exists() && $default !== false | 59 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme(), get_stylesheet(), ->get_stylesheet() |
!wp_installing() && apply_filters() && is_child_theme() && ->exists() | 59 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists(), wp_get_theme(), ->exists(), switch_theme() |
!wp_installing() && apply_filters() && is_child_theme() && !->exists() && $default === false | 60 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme() |
!wp_installing() && apply_filters() && !->exists() && $default !== false | 61 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme(), get_stylesheet(), ->get_stylesheet(), ->get_stylesheet(), switch_theme() |
!wp_installing() && apply_filters() && is_child_theme() && !->exists() && $default !== false | 64 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme(), get_stylesheet(), ->get_stylesheet(), ->get_stylesheet(), switch_theme() |
!wp_installing() && apply_filters() && is_child_theme() && !->exists() && $default !== false | 66 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme(), get_stylesheet(), ->get_stylesheet() |
!wp_installing() && apply_filters() && is_child_theme() && !->exists() && $default !== false | 71 | wp_installing(), apply_filters(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), get_template_directory(), file_exists(), is_child_theme(), get_stylesheet_directory(), file_exists(), wp_get_theme(), ->exists(), ::WP_Theme(), get_stylesheet(), ->get_stylesheet(), ->get_stylesheet(), switch_theme() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 79 instructions, 4–71 executed per call, 11 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 · 1
One hook fires while validate_current_theme() runs, in this order:
- apply_filters( validate_current_theme )filterline 906 (+8 into the body)
Filters whether to validate the active theme.
Uses · 9
- wp_installing()Checks or sets whether WordPress is in "installation" mode.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_template_directory()Retrieves template directory path for the active theme.
- is_child_theme()Whether a child theme is in use.
- get_stylesheet_directory()Retrieves stylesheet directory path for the active theme.
- wp_get_theme()Gets a WP_Theme object for a theme.
- switch_theme()Switches the theme.
- get_stylesheet()Retrieves name of the current stylesheet.
- WP_Theme::get_core_default_theme()Determines the latest WordPress default theme that is installed.
Used by · 1
- WP_Customize_Manager::after_setup_theme()Callback to validate a theme once it is loaded
Source code
function validate_current_theme() { /** * Filters whether to validate the active theme. * * @since 2.7.0 * * @param bool $validate Whether to validate the active theme. Default true. */ if ( wp_installing() || ! apply_filters( 'validate_current_theme', true ) ) { return true; } if ( ! file_exists( get_template_directory() . '/templates/index.html' ) && ! file_exists( get_template_directory() . '/block-templates/index.html' ) // Deprecated path support since 5.9.0. && ! file_exists( get_template_directory() . '/index.php' ) ) { // Invalid. } elseif ( ! file_exists( get_template_directory() . '/style.css' ) ) { // Invalid. } elseif ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) { // Invalid. } else { // Valid. return true; } $default = wp_get_theme( WP_DEFAULT_THEME ); if ( $default->exists() ) { switch_theme( WP_DEFAULT_THEME ); return false; } /** * If we're in an invalid state but WP_DEFAULT_THEME doesn't exist, * switch to the latest core default theme that's installed. * * If it turns out that this latest core default theme is our current * theme, then there's nothing we can do about that, so we have to bail, * rather than going into an infinite loop. (This is why there are * checks against WP_DEFAULT_THEME above, also.) We also can't do anything * if it turns out there is no default theme installed. (That's `false`.) */ $default = WP_Theme::get_core_default_theme(); if ( false === $default || get_stylesheet() === $default->get_stylesheet() ) { return true; } switch_theme( $default->get_stylesheet() ); return false;}Changelog
Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
index.php template.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.