search_theme_directories

Search all registered theme directories for complete and valid themes.

Description

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

Returns (array|false)

Valid themes found

Parameters (1)

0. $force — Optional. (bool) => false
Whether to force a new directory scan. Defaults to false.

Usage

  1. if ( !function_exists( 'search_theme_directories' ) ) { 
  2. require_once ABSPATH . WPINC . '/theme.php'; 
  3.  
  4. // Optional. Whether to force a new directory scan. Defaults to false. 
  5. $force = false; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = search_theme_directories($force); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/theme.php  
  1. function search_theme_directories( $force = false ) { 
  2. global $wp_theme_directories; 
  3. static $found_themes = null; 
  4.  
  5. if ( empty( $wp_theme_directories ) ) 
  6. return false; 
  7.  
  8. if ( ! $force && isset( $found_themes ) ) 
  9. return $found_themes; 
  10.  
  11. $found_themes = array(); 
  12.  
  13. $wp_theme_directories = (array) $wp_theme_directories; 
  14. $relative_theme_roots = array(); 
  15.  
  16. // Set up maybe-relative, maybe-absolute array of theme directories. 
  17. // We always want to return absolute, but we need to cache relative 
  18. // to use in get_theme_root(). 
  19. foreach ( $wp_theme_directories as $theme_root ) { 
  20. if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) ) 
  21. $relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root; 
  22. else 
  23. $relative_theme_roots[ $theme_root ] = $theme_root; 
  24.  
  25. /** 
  26. * Filters whether to get the cache of the registered theme directories. 
  27. * @since 3.4.0 
  28. * @param bool $cache_expiration Whether to get the cache of the theme directories. Default false. 
  29. * @param string $cache_directory Directory to be searched for the cache. 
  30. */ 
  31. if ( $cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' ) ) { 
  32. $cached_roots = get_site_transient( 'theme_roots' ); 
  33. if ( is_array( $cached_roots ) ) { 
  34. foreach ( $cached_roots as $theme_dir => $theme_root ) { 
  35. // A cached theme root is no longer around, so skip it. 
  36. if ( ! isset( $relative_theme_roots[ $theme_root ] ) ) 
  37. continue; 
  38. $found_themes[ $theme_dir ] = array( 
  39. 'theme_file' => $theme_dir . '/style.css',  
  40. 'theme_root' => $relative_theme_roots[ $theme_root ], // Convert relative to absolute. 
  41. ); 
  42. return $found_themes; 
  43. if ( ! is_int( $cache_expiration ) ) 
  44. $cache_expiration = 1800; // half hour 
  45. } else { 
  46. $cache_expiration = 1800; // half hour 
  47.  
  48. /** Loop the registered theme directories and extract all themes */ 
  49. foreach ( $wp_theme_directories as $theme_root ) { 
  50.  
  51. // Start with directories in the root of the current theme directory. 
  52. $dirs = @ scandir( $theme_root ); 
  53. if ( ! $dirs ) { 
  54. trigger_error( "$theme_root is not readable", E_USER_NOTICE ); 
  55. continue; 
  56. foreach ( $dirs as $dir ) { 
  57. if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' ) 
  58. continue; 
  59. if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) { 
  60. // wp-content/themes/a-single-theme 
  61. // wp-content/themes is $theme_root, a-single-theme is $dir 
  62. $found_themes[ $dir ] = array( 
  63. 'theme_file' => $dir . '/style.css',  
  64. 'theme_root' => $theme_root,  
  65. ); 
  66. } else { 
  67. $found_theme = false; 
  68. // wp-content/themes/a-folder-of-themes/* 
  69. // wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs 
  70. $sub_dirs = @ scandir( $theme_root . '/' . $dir ); 
  71. if ( ! $sub_dirs ) { 
  72. trigger_error( "$theme_root/$dir is not readable", E_USER_NOTICE ); 
  73. continue; 
  74. foreach ( $sub_dirs as $sub_dir ) { 
  75. if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || $dir[0] == '.' || $dir == 'CVS' ) 
  76. continue; 
  77. if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) ) 
  78. continue; 
  79. $found_themes[ $dir . '/' . $sub_dir ] = array( 
  80. 'theme_file' => $dir . '/' . $sub_dir . '/style.css',  
  81. 'theme_root' => $theme_root,  
  82. ); 
  83. $found_theme = true; 
  84. // Never mind the above, it's just a theme missing a style.css. 
  85. // Return it; WP_Theme will catch the error. 
  86. if ( ! $found_theme ) 
  87. $found_themes[ $dir ] = array( 
  88. 'theme_file' => $dir . '/style.css',  
  89. 'theme_root' => $theme_root,  
  90. ); 
  91.  
  92. asort( $found_themes ); 
  93.  
  94. $theme_roots = array(); 
  95. $relative_theme_roots = array_flip( $relative_theme_roots ); 
  96.  
  97. foreach ( $found_themes as $theme_dir => $theme_data ) { 
  98. $theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ]; // Convert absolute to relative. 
  99.  
  100. if ( $theme_roots != get_site_transient( 'theme_roots' ) ) 
  101. set_site_transient( 'theme_roots', $theme_roots, $cache_expiration ); 
  102.  
  103. return $found_themes;