wppaste
WordPress

search_theme_directories( bool $force = false ): array|false

Since
2.9.0
Source
wp-includes/theme.php:449
Searches all registered theme directories for complete and valid themes.

Compatibility

WordPress
since 2.9.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.

Parameters

$forcebooloptional
Whether to force a new directory scan. Default false.Default: false

Return value

array|false
Valid themes found on success, false on failure.

Performance profile

How much work a call to search_theme_directories() 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 stored settings via get_site_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
6–59

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

Plugin surface
1 hook

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

Called by
5

5 places 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
  • transienttransientget_site_transient()called directly
  • cacheobject cachewp_cache_get()one call below search_theme_directories()
  • optionnetwork optionget_site_option()one call below search_theme_directories()

Further down the call graph this can also reach serialize 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 · 6 distinct outcomes

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

WhenInstructionsCalls it makes
always6–9none
!empty($wp_theme_directories) && is_array($cached_roots)28–32apply_filters(), get_site_transient()
!empty($wp_theme_directories) && $theme_roots === false40–45apply_filters(), asort(), array_flip(), get_site_transient()
!empty($wp_theme_directories) && $theme_roots !== false45–50apply_filters(), asort(), array_flip(), get_site_transient(), set_site_transient()
!empty($wp_theme_directories) && !is_array($cached_roots) && $theme_roots === false45–54apply_filters(), get_site_transient(), asort(), array_flip(), get_site_transient()
!empty($wp_theme_directories) && !is_array($cached_roots) && $theme_roots !== false50–59apply_filters(), get_site_transient(), asort(), array_flip(), get_site_transient(), set_site_transient()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1996–5932
8.51996–5932
8.41996–59326 fewer instructions than PHP 8.3
8.32056–5932
8.22056–5932
8.12056–5932
7.42056–5932

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 search_theme_directories() runs, in this order:

  1. apply_filters( wp_cache_themes_persistently )filterline 487 (+38 into the body)

    Filters whether to get the cache of the registered theme directories.

Uses · 5

Used by · 5

Source code

function search_theme_directories( $force = false ) {	global $wp_theme_directories;	static $found_themes = null; 	if ( empty( $wp_theme_directories ) ) {		return false;	} 	if ( ! $force && isset( $found_themes ) ) {		return $found_themes;	} 	$found_themes = array(); 	$wp_theme_directories = (array) $wp_theme_directories;	$relative_theme_roots = array(); 	/*	 * Set up maybe-relative, maybe-absolute array of theme directories.	 * We always want to return absolute, but we need to cache relative	 * to use in get_theme_root().	 */	foreach ( $wp_theme_directories as $theme_root ) {		if ( str_starts_with( $theme_root, WP_CONTENT_DIR ) ) {			$relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root;		} else {			$relative_theme_roots[ $theme_root ] = $theme_root;		}	} 	/**	 * Filters whether to get the cache of the registered theme directories.	 *	 * @since 3.4.0	 *	 * @param bool   $cache_expiration Whether to get the cache of the theme directories. Default false.	 * @param string $context          The class or function name calling the filter.	 */	$cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' ); 	if ( $cache_expiration ) {		$cached_roots = get_site_transient( 'theme_roots' );		if ( is_array( $cached_roots ) ) {			foreach ( $cached_roots as $theme_dir => $theme_root ) {				// A cached theme root is no longer around, so skip it.				if ( ! isset( $relative_theme_roots[ $theme_root ] ) ) {					continue;				}				$found_themes[ $theme_dir ] = array(					'theme_file' => $theme_dir . '/style.css',					'theme_root' => $relative_theme_roots[ $theme_root ], // Convert relative to absolute.				);			}			return $found_themes;		}		if ( ! is_int( $cache_expiration ) ) {			$cache_expiration = 30 * MINUTE_IN_SECONDS;		}	} else {		$cache_expiration = 30 * MINUTE_IN_SECONDS;	} 	/* Loop the registered theme directories and extract all themes */	foreach ( $wp_theme_directories as $theme_root ) { 		// Start with directories in the root of the active theme directory.		$dirs = @ scandir( $theme_root );		if ( ! $dirs ) {			wp_trigger_error( __FUNCTION__, "$theme_root is not readable" );			continue;		}		foreach ( $dirs as $dir ) {			if ( ! is_dir( $theme_root . '/' . $dir ) || '.' === $dir[0] || 'CVS' === $dir ) {				continue;			}			if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {				/*				 * wp-content/themes/a-single-theme				 * wp-content/themes is $theme_root, a-single-theme is $dir.				 */

Changelog

Introduced in 2.9.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.