wp_prepare_themes_for_js( WP_Theme[] $themes = null ): array
- Since
- 3.8.0
- Source
wp-admin/includes/theme.php:647
Compatibility
- WordPress
- since 3.8.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
$themesWP_Theme[]optional- Array of theme objects to prepare.
Defaults to all allowed themes.Default:null
Return value
array- An associative array of theme data, sorted by name.
Performance profile
How much work a call to wp_prepare_themes_for_js() 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
- 48–55
- Plugin surface
- 2 hooks
- Called by
- 1
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.5, depending on the branch taken. The body compiles to 368.
Third-party callbacks on 'pre_prepare_themes_for_js', 'wp_prepare_themes_for_js' run inside this call, and their cost is not bounded by anything here.
1 place 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 - optionnetwork option
get_site_option()called directly - cacheobject cache
wp_cache_get()one call below wp_prepare_themes_for_js()
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 · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_prepare_themes_for_js() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($prepared_themes) && $themes !== null && is_multisite() | 48–55 | get_stylesheet(), apply_filters(), is_multisite(), ::WP_Theme(), get_site_option(), apply_filters(), array_values(), array_filter() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 365 | 48–55 | 32 | 3 fewer instructions than PHP 8.5 |
| 8.5 | 368 | 48–55 | 32 | |
| 8.4 | 368 | 48–55 | 32 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 371 | 48–55 | 32 | |
| 8.2 | 371 | 48–55 | 32 | |
| 8.1 | 371 | 48–55 | 32 | |
| 7.4 | 371 | 48–55 | 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 · 2
2 hooks fire while wp_prepare_themes_for_js() runs, in this order:
- apply_filters( pre_prepare_themes_for_js )filterline 662 (+15 into the body)
Filters theme data before it is prepared for JavaScript.
- apply_filters( wp_prepare_themes_for_js )filterline 812 (+165 into the body)
Filters the themes prepared for JavaScript, for themes.php.
Uses · 23
- get_stylesheet()Retrieves name of the current stylesheet.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_get_themes()Returns an array of WP_Theme objects based on the arguments.
- wp_get_theme()Gets a WP_Theme object for a theme.
- is_multisite()Determines whether Multisite is enabled.
- current_user_can()Returns whether the current user has the specified capability.
- get_site_transient()Retrieves the value of a site transient.
- get_site_option()Retrieve an option value for the current network based on name of option.
- admin_url()Retrieves the URL to the admin area for the current site.
- add_query_arg()Retrieves a modified URL query string.
- wp_customize_url()Returns a URL to load the Customizer.
- sanitize_url()Sanitizes a URL for database or redirect usage.
Show all 23
- remove_query_arg()Removes an item or items from a query string.
- wp_removable_query_args()Returns an array of single-use query variable names that can be removed from a URL.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- esc_url()Checks and cleans a URL.
- wp_is_auto_update_forced_for_item()Checks whether auto-updates are forced for an item.
- is_wp_version_compatible()Checks compatibility with the current WordPress version.
- is_php_version_compatible()Checks compatibility with the current PHP version.
- get_theme_update_available()Retrieves the update link if there is a theme update available.
- wp_nonce_url()Retrieves URL with nonce added to URL query.
- wp_is_auto_update_enabled_for_type()Checks whether auto-updates are enabled.
- WP_Theme::sort_by_name()Sorts themes by name.
Used by · 1
- WP_Customize_Manager::handle_load_themes_request()Loads themes into the theme browsing/installation UI.
Source code
function wp_prepare_themes_for_js( $themes = null ) { $current_theme = get_stylesheet(); /** * Filters theme data before it is prepared for JavaScript. * * Passing a non-empty array will result in wp_prepare_themes_for_js() returning * early with that value instead. * * @since 4.2.0 * * @param array $prepared_themes An associative array of theme data. Default empty array. * @param WP_Theme[]|null $themes An array of theme objects to prepare, if any. * @param string $current_theme The active theme slug. */ $prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme ); if ( ! empty( $prepared_themes ) ) { return $prepared_themes; } // Make sure the active theme is listed first. $prepared_themes[ $current_theme ] = array(); if ( null === $themes ) { $themes = wp_get_themes( array( 'allowed' => true ) ); if ( ! isset( $themes[ $current_theme ] ) ) { $themes[ $current_theme ] = wp_get_theme(); } } $updates = array(); $no_updates = array(); if ( ! is_multisite() && current_user_can( 'update_themes' ) ) { $updates_transient = get_site_transient( 'update_themes' ); if ( isset( $updates_transient->response ) ) { $updates = $updates_transient->response; } if ( isset( $updates_transient->no_update ) ) { $no_updates = $updates_transient->no_update; } } WP_Theme::sort_by_name( $themes ); $parents = array(); $auto_updates = (array) get_site_option( 'auto_update_themes', array() ); foreach ( $themes as $theme ) { $slug = $theme->get_stylesheet(); $encoded_slug = urlencode( $slug ); $parent = false; if ( $theme->parent() ) { $parent = $theme->parent(); $parents[ $slug ] = $parent->get_stylesheet(); $parent = $parent->display( 'Name' ); } $customize_action = null; $can_edit_theme_options = current_user_can( 'edit_theme_options' ); $can_customize = current_user_can( 'customize' ); $is_block_theme = $theme->is_block_theme(); if ( $is_block_theme && $can_edit_theme_options ) { $customize_action = admin_url( 'site-editor.php' ); if ( $current_theme !== $slug ) { $customize_action = add_query_arg( 'wp_theme_preview', $slug, $customize_action ); } } elseif ( ! $is_block_theme && $can_customize && $can_edit_theme_options ) { $customize_action = wp_customize_url( $slug ); } if ( null !== $customize_action ) { $customize_action = add_query_arg( array( 'return' => urlencode( sanitize_url( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ), ), $customize_actionChangelog
Introduced in 3.8.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 7.0.4 tag, from
src/wp-admin/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.