wp_get_themes

Returns an array of WP_Theme objects based on the arguments.

Description

(array) wp_get_themes( (array) $args = array() ); 

Despite advances over get_themes(), this function is quite expensive, and grows linearly with additional themes. Stick to wp_get_theme() if possible.

Returns (array)

Array of WP_Theme objects.

Parameters (1)

0. $args — Optional. (array) => array()
The search arguments. Optional. - errors mixed True to return themes with errors, false to return themes without errors, null to return all themes. Defaults to false. - allowed mixed (Multisite) True to return only allowed themes for a site. False to return only disallowed themes for a site. site to return only site-allowed themes. network to return only network-allowed themes. Null to return all themes. Defaults to null. - blog_id int (Multisite) The blog ID used to calculate which themes are allowed. Defaults to 0, synonymous for the current blog.

Usage

  1. if ( !function_exists( 'wp_get_themes' ) ) { 
  2. require_once ABSPATH . WPINC . '/theme.php'; 
  3. $args = array(); 
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_get_themes($args); 
  7.  

Defined (1)

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

/wp-includes/theme.php  
  1. function wp_get_themes( $args = array() ) { 
  2. global $wp_theme_directories; 
  3.  
  4. $defaults = array( 'errors' => false, 'allowed' => null, 'blog_id' => 0 ); 
  5. $args = wp_parse_args( $args, $defaults ); 
  6.  
  7. $theme_directories = search_theme_directories(); 
  8.  
  9. if ( count( $wp_theme_directories ) > 1 ) { 
  10. // Make sure the current theme wins out, in case search_theme_directories() picks the wrong 
  11. // one in the case of a conflict. (Normally, last registered theme root wins.) 
  12. $current_theme = get_stylesheet(); 
  13. if ( isset( $theme_directories[ $current_theme ] ) ) { 
  14. $root_of_current_theme = get_raw_theme_root( $current_theme ); 
  15. if ( ! in_array( $root_of_current_theme, $wp_theme_directories ) ) 
  16. $root_of_current_theme = WP_CONTENT_DIR . $root_of_current_theme; 
  17. $theme_directories[ $current_theme ]['theme_root'] = $root_of_current_theme; 
  18.  
  19. if ( empty( $theme_directories ) ) 
  20. return array(); 
  21.  
  22. if ( is_multisite() && null !== $args['allowed'] ) { 
  23. $allowed = $args['allowed']; 
  24. if ( 'network' === $allowed ) 
  25. $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_network() ); 
  26. elseif ( 'site' === $allowed ) 
  27. $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_site( $args['blog_id'] ) ); 
  28. elseif ( $allowed ) 
  29. $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) ); 
  30. else 
  31. $theme_directories = array_diff_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) ); 
  32.  
  33. $themes = array(); 
  34. static $_themes = array(); 
  35.  
  36. foreach ( $theme_directories as $theme => $theme_root ) { 
  37. if ( isset( $_themes[ $theme_root['theme_root'] . '/' . $theme ] ) ) 
  38. $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ]; 
  39. else 
  40. $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ] = new WP_Theme( $theme, $theme_root['theme_root'] ); 
  41.  
  42. if ( null !== $args['errors'] ) { 
  43. foreach ( $themes as $theme => $wp_theme ) { 
  44. if ( $wp_theme->errors() != $args['errors'] ) 
  45. unset( $themes[ $theme ] ); 
  46.  
  47. return $themes;