Title: search_theme_directories
Published: April 25, 2014
Last modified: August 20, 2026

---

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

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#user-contributed-notes)

Searches all registered theme directories for complete and valid themes.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#parameters)󠁿

 `$force`booloptional

Whether to force a new directory scan.

Default:`false`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#return)󠁿

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

## 󠀁[Source](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#source)󠁿

    ```php
    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.
    				 */
    				$found_themes[ $dir ] = array(
    					'theme_file' => $dir . '/style.css',
    					'theme_root' => $theme_root,
    				);
    			} else {
    				$found_theme = false;
    				/*
    				 * wp-content/themes/a-folder-of-themes/*
    				 * wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs.
    				 */
    				$sub_dirs = @ scandir( $theme_root . '/' . $dir );
    				if ( ! $sub_dirs ) {
    					wp_trigger_error( __FUNCTION__, "$theme_root/$dir is not readable" );
    					continue;
    				}
    				foreach ( $sub_dirs as $sub_dir ) {
    					if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || '.' === $dir[0] || 'CVS' === $dir ) {
    						continue;
    					}
    					if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) ) {
    						continue;
    					}
    					$found_themes[ $dir . '/' . $sub_dir ] = array(
    						'theme_file' => $dir . '/' . $sub_dir . '/style.css',
    						'theme_root' => $theme_root,
    					);
    					$found_theme                           = true;
    				}
    				/*
    				 * Never mind the above, it's just a theme missing a style.css.
    				 * Return it; WP_Theme will catch the error.
    				 */
    				if ( ! $found_theme ) {
    					$found_themes[ $dir ] = array(
    						'theme_file' => $dir . '/style.css',
    						'theme_root' => $theme_root,
    					);
    				}
    			}
    		}
    	}

    	asort( $found_themes );

    	$theme_roots          = array();
    	$relative_theme_roots = array_flip( $relative_theme_roots );

    	foreach ( $found_themes as $theme_dir => $theme_data ) {
    		$theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ]; // Convert absolute to relative.
    	}

    	if ( get_site_transient( 'theme_roots' ) !== $theme_roots ) {
    		set_site_transient( 'theme_roots', $theme_roots, $cache_expiration );
    	}

    	return $found_themes;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/theme.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/theme.php#L449)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/theme.php#L449-L585)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#hooks)󠁿

 [apply_filters( ‘wp_cache_themes_persistently’, bool $cache_expiration, string $context )](https://developer.wordpress.org/reference/hooks/wp_cache_themes_persistently/)

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

## 󠀁[Related](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_trigger_error()](https://developer.wordpress.org/reference/functions/wp_trigger_error/)`wp-includes/functions.php` |

Generates a user-level error/warning/notice/deprecation message.

  | 
| [set_site_transient()](https://developer.wordpress.org/reference/functions/set_site_transient/)`wp-includes/option.php` |

Sets/updates the value of a site transient.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [get_site_transient()](https://developer.wordpress.org/reference/functions/get_site_transient/)`wp-includes/option.php` |

Retrieves the value of a site transient.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#)

| Used by | Description | 
| [wp_ajax_query_themes()](https://developer.wordpress.org/reference/functions/wp_ajax_query_themes/)`wp-admin/includes/ajax-actions.php` |

Handles getting themes from [themes_api()](https://developer.wordpress.org/reference/functions/themes_api/) via AJAX.

  | 
| [get_theme_roots()](https://developer.wordpress.org/reference/functions/get_theme_roots/)`wp-includes/theme.php` |

Retrieves theme roots.

  | 
| [wp_get_themes()](https://developer.wordpress.org/reference/functions/wp_get_themes/)`wp-includes/theme.php` |

Returns an array of [WP_Theme](https://developer.wordpress.org/reference/classes/wp_theme/) objects based on the arguments.

  | 
| [wp_clean_themes_cache()](https://developer.wordpress.org/reference/functions/wp_clean_themes_cache/)`wp-includes/theme.php` |

Clears the cache held by [get_theme_roots()](https://developer.wordpress.org/reference/functions/get_theme_roots/) and [WP_Theme](https://developer.wordpress.org/reference/classes/wp_theme/).

  | 
| [WP_Theme::__construct()](https://developer.wordpress.org/reference/classes/wp_theme/__construct/)`wp-includes/class-wp-theme.php` |

Constructor for [WP_Theme](https://developer.wordpress.org/reference/classes/wp_theme/).

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.9.0](https://developer.wordpress.org/reference/since/2.9.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/search_theme_directories/?output_format=md#comment-content-5968)
 2.   [changwoo](https://profiles.wordpress.org/changwoo/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/search_theme_directories/#comment-5968)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fsearch_theme_directories%2F%23comment-5968)
    Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fsearch_theme_directories%2F%23comment-5968)
 4. [The official documentation](https://developer.wordpress.org/themes/basics/main-stylesheet-style-css/#location)
    states that “the style.css file must be located in the root directory of the theme,
    not in a subdirectory.”. However, looking at the implementation of this function,
    we can see that this is not true. You can place the style.css and functions.php
    files in one of direct subdirectories of the root directory. The core will find
    those files and recognize the subdirectory as the theme’s root directory.
 5.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fsearch_theme_directories%2F%3Freplytocom%3D5968%23feedback-editor-5968)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fsearch_theme_directories%2F)
before being able to contribute a note or feedback.