search_theme_directories( bool $force = false ): array|false
- Since
- 2.9.0
- Source
wp-includes/theme.php:449
Compatibility
- WordPress
- since 2.9.0
- PHP
- 7.4–8.4
- 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.4.
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
- Scaling
- Scales with input
- Instructions
- 6–59
- Plugin surface
- 1 hook
- Called by
- 5
Reads stored settings via get_site_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 199.
Third-party callbacks on 'wp_cache_themes_persistently' run inside this call, and their cost is not bounded by anything here.
5 places 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 - transienttransient
get_site_transient()called directly - cacheobject cache
wp_cache_get()one call below search_theme_directories() - optionnetwork option
get_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.4.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–9 | none |
!empty($wp_theme_directories) && is_array($cached_roots) | 28–32 | apply_filters(), get_site_transient() |
!empty($wp_theme_directories) && $theme_roots === false | 40–45 | apply_filters(), asort(), array_flip(), get_site_transient() |
!empty($wp_theme_directories) && $theme_roots !== false | 45–50 | apply_filters(), asort(), array_flip(), get_site_transient(), set_site_transient() |
!empty($wp_theme_directories) && !is_array($cached_roots) && $theme_roots === false | 45–54 | apply_filters(), get_site_transient(), asort(), array_flip(), get_site_transient() |
!empty($wp_theme_directories) && !is_array($cached_roots) && $theme_roots !== false | 50–59 | apply_filters(), get_site_transient(), asort(), array_flip(), get_site_transient(), set_site_transient() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.4 | 199 | 6–59 | 32 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 205 | 6–59 | 32 | |
| 8.2 | 205 | 6–59 | 32 | |
| 8.1 | 205 | 6–59 | 32 | |
| 7.4 | 205 | 6–59 | 32 |
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:
- 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
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_site_transient()Retrieves the value of a site transient.
- wp_trigger_error()Generates a user-level error/warning/notice/deprecation message.
- set_site_transient()Sets/updates the value of a site transient.
Used by · 5
- WP_Theme::__construct()Constructor for WP_Theme.
- get_theme_roots()Retrieves theme roots.
- wp_ajax_query_themes()Handles getting themes from themes_api() via AJAX.
- wp_clean_themes_cache()Clears the cache held by get_theme_roots() and WP_Theme.
- wp_get_themes()Returns an array of WP_Theme objects based on the arguments.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.