wp_get_themes( array $args = array() ): WP_Theme[]
- Since
- 3.4.0
- Source
wp-includes/theme.php:35
Description
Despite advances over get_themes(), this function is quite expensive, and grows linearly with additional themes. Stick to wp_get_theme() if possible.
Compatibility
- WordPress
- since 3.4.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
$argsarrayoptional- The search arguments.Default:
array()$errorsmixeddefault: falseTrue to return themes with errors, false to return themes without errors, null to return all themes.$allowedmixeddefault: null(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.$blog_idintdefault: 0, synonymous for the current blog(Multisite) The blog ID used to calculate which themes are allowed.
Return value
WP_Theme[]- Array of WP_Theme objects.
Performance profile
How much work a call to wp_get_themes() 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
- 16–71
- Plugin surface
- None
- Called by
- 16
Reads stored settings via get_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.5, depending on the branch taken. The body compiles to 138.
Nothing here hands control to plugin code.
16 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()one call below wp_get_themes() - transienttransient
get_site_transient()one call below wp_get_themes() - optionoption read or write
get_option()one call below wp_get_themes()
Further down the call graph this can also reach cache, 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 · 12 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_themes() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($theme_directories) | 16–19 | wp_parse_args(), search_theme_directories() |
is_array($wp_theme_directories) && !isset($theme_directories[$current_theme]) && empty($theme_directories) | 24 | wp_parse_args(), search_theme_directories(), get_stylesheet() |
!empty($theme_directories) | 26–36 | wp_parse_args(), search_theme_directories(), is_multisite() |
is_array($wp_theme_directories) && !isset($theme_directories[$current_theme]) && !empty($theme_directories) | 34–41 | wp_parse_args(), search_theme_directories(), get_stylesheet(), is_multisite() |
is_array($wp_theme_directories) && isset($theme_directories[$current_theme]) && empty($theme_directories) | 34–37 | wp_parse_args(), search_theme_directories(), get_stylesheet(), get_raw_theme_root() |
!empty($theme_directories) && is_multisite() | 40–53 | wp_parse_args(), search_theme_directories(), is_multisite(), ::WP_Theme(), array_intersect_key() |
is_array($wp_theme_directories) && isset($theme_directories[$current_theme]) && !empty($theme_directories) | 44–54 | wp_parse_args(), search_theme_directories(), get_stylesheet(), get_raw_theme_root(), is_multisite() |
!empty($theme_directories) && is_multisite() && $allowed !== "network" && $allowed !== "site" && !$args | 45–52 | wp_parse_args(), search_theme_directories(), is_multisite(), ::WP_Theme(), array_diff_key() |
is_array($wp_theme_directories) && !isset($theme_directories[$current_theme]) && !empty($theme_directories) && is_multisite() | 48–58 | wp_parse_args(), search_theme_directories(), get_stylesheet(), is_multisite(), ::WP_Theme(), array_intersect_key() |
is_array($wp_theme_directories) && !isset($theme_directories[$current_theme]) && !empty($theme_directories) && is_multisite() && $allowed !== "network" && $allowed !== "site" && !$args | 53–57 | wp_parse_args(), search_theme_directories(), get_stylesheet(), is_multisite(), ::WP_Theme(), array_diff_key() |
is_array($wp_theme_directories) && isset($theme_directories[$current_theme]) && !empty($theme_directories) && is_multisite() | 58–71 | wp_parse_args(), search_theme_directories(), get_stylesheet(), get_raw_theme_root(), is_multisite(), ::WP_Theme(), array_intersect_key() |
is_array($wp_theme_directories) && isset($theme_directories[$current_theme]) && !empty($theme_directories) && is_multisite() && $allowed !== "network" && $allowed !== "site" && !$args | 63–70 | wp_parse_args(), search_theme_directories(), get_stylesheet(), get_raw_theme_root(), is_multisite(), ::WP_Theme(), array_diff_key() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 138 | 16–71 | 17 | |
| 8.5 | 138 | 16–71 | 17 | |
| 8.4 | 138 | 16–71 | 17 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 141 | 16–74 | 17 | |
| 8.2 | 141 | 16–74 | 17 | |
| 8.1 | 141 | 16–74 | 17 | |
| 7.4 | 141 | 16–74 | 17 |
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.
Uses · 9
- wp_parse_args()Merges user defined arguments into defaults array.
- search_theme_directories()Searches all registered theme directories for complete and valid themes.
- get_stylesheet()Retrieves name of the current stylesheet.
- get_raw_theme_root()Gets the raw theme root relative to the content directory with no filters applied.
- is_multisite()Determines whether Multisite is enabled.
- WP_Theme::get_allowed_on_network()Returns array of stylesheet names of themes allowed on the network.
- WP_Theme::get_allowed_on_site()Returns array of stylesheet names of themes allowed on the site.
- WP_Theme::get_allowed()Returns array of stylesheet names of themes allowed on the site or network.
- WP_Theme::__construct()Constructor for WP_Theme.
Used by · 16
- Theme_Installer_Skin::do_overwrite()Checks if the theme can be overwritten and outputs the HTML for overwriting a theme on upload.
- WP_Customize_Manager::handle_load_themes_request()Loads themes into the theme browsing/installation UI.
- WP_Debug_Data::get_wp_themes_inactive()Gets the WordPress inactive themes section of the debug data.
- WP_MS_Themes_List_Table::prepare_items()
- WP_REST_Themes_Controller::get_items()Retrieves a collection of themes.
- WP_Site_Health::get_test_theme_version()Tests if themes are outdated, or unnecessary.
- WP_Theme::get_allowed_on_site()Returns array of stylesheet names of themes allowed on the site.
- WP_Themes_List_Table::prepare_items()
- get_allowed_themes()Get the allowed themes for the current site.
- get_broken_themes()Retrieves a list of broken themes.
- get_themes()Retrieve list of themes with theme data in theme directory.
- upgrade_network()Executes network-level upgrade routines.
Show all 16
- wp_ajax_toggle_auto_updates()Handles enabling or disable plugin and theme auto-updates via AJAX.
- wp_clean_themes_cache()Clears the cache held by get_theme_roots() and WP_Theme.
- wp_prepare_themes_for_js()Prepares themes for JavaScript.
- wp_update_themes()Checks for available updates to themes based on the latest versions hosted on WordPress.org.
Source code
function wp_get_themes( $args = array() ) { global $wp_theme_directories; $defaults = array( 'errors' => false, 'allowed' => null, 'blog_id' => 0, ); $args = wp_parse_args( $args, $defaults ); $theme_directories = search_theme_directories(); if ( is_array( $wp_theme_directories ) && count( $wp_theme_directories ) > 1 ) { /* * Make sure the active theme wins out, in case search_theme_directories() picks the wrong * one in the case of a conflict. (Normally, last registered theme root wins.) */ $current_theme = get_stylesheet(); if ( isset( $theme_directories[ $current_theme ] ) ) { $root_of_current_theme = get_raw_theme_root( $current_theme ); if ( ! in_array( $root_of_current_theme, $wp_theme_directories, true ) ) { $root_of_current_theme = WP_CONTENT_DIR . $root_of_current_theme; } $theme_directories[ $current_theme ]['theme_root'] = $root_of_current_theme; } } if ( empty( $theme_directories ) ) { return array(); } if ( is_multisite() && null !== $args['allowed'] ) { $allowed = $args['allowed']; if ( 'network' === $allowed ) { $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_network() ); } elseif ( 'site' === $allowed ) { $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_site( $args['blog_id'] ) ); } elseif ( $allowed ) { $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) ); } else { $theme_directories = array_diff_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) ); } } $themes = array(); static $_themes = array(); foreach ( $theme_directories as $theme => $theme_root ) { if ( isset( $_themes[ $theme_root['theme_root'] . '/' . $theme ] ) ) { $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ]; } else { $themes[ $theme ] = new WP_Theme( $theme, $theme_root['theme_root'] ); $_themes[ $theme_root['theme_root'] . '/' . $theme ] = $themes[ $theme ]; } } if ( null !== $args['errors'] ) { foreach ( $themes as $theme => $wp_theme ) { if ( (bool) $wp_theme->errors() !== $args['errors'] ) { unset( $themes[ $theme ] ); } } } return $themes;}Changelog
Introduced in 3.4.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.