wppaste
WordPress

validate_current_theme(): bool

Since
1.5.0, 6.0.0
Source
wp-includes/theme.php:898
Checks that the active theme has the required files.

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

Reads or writes the filesystem via file_exists().

Scaling
Constant

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

Instructions
4–71

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 79.

Plugin surface
1 hook

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

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfile_exists()called directly
  • optionoption read or writeupdate_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.

WhenInstructionsCalls it makes
wp_installing()4wp_installing()
!wp_installing() && !apply_filters()9wp_installing(), apply_filters()
!wp_installing() && apply_filters() && file_exists() && !is_child_theme()26wp_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()33wp_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()33wp_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()35wp_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 === false36wp_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()40wp_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()40wp_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 !== false42wp_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()42wp_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 === false43wp_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()45wp_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 === false46wp_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 !== false47wp_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()47wp_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 !== false49wp_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()49wp_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 === false50wp_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 !== false52wp_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()52wp_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 === false53wp_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 !== false54wp_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 !== false56wp_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 !== false57wp_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 !== false59wp_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()59wp_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 === false60wp_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 !== false61wp_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 !== false64wp_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 !== false66wp_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 !== false71wp_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:

  1. apply_filters( validate_current_theme )filterline 906 (+8 into the body)

    Filters whether to validate the active theme.

Uses · 9

Used by · 1

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.

  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.

6.0.0
Removed the requirement for block themes to have an index.php template.from the docblock
1.5.0
Introduced.from the docblock

About 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.