wppaste
WordPress

wp_prepare_themes_for_js( WP_Theme[] $themes = null ): array

Since
3.8.0
Source
wp-admin/includes/theme.php:651
Prepares themes for JavaScript.

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

Reads stored settings via get_site_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
48–55

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 374.

Plugin surface
2 hooks

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.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • transienttransientget_site_transient()called directly
  • optionnetwork optionget_site_option()called directly
  • cacheobject cachewp_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.

WhenInstructionsCalls it makes
empty($prepared_themes) && $themes !== null && is_multisite()48–55get_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

PHPCompiledExecutedBranchesNotes
8.6-dev37148–55323 fewer instructions than PHP 8.5
8.537448–5532
8.437448–55323 fewer instructions than PHP 8.3
8.337748–5532
8.237748–5532
8.137748–55322 fewer instructions than PHP 7.4
7.437948–5532

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:

  1. apply_filters( pre_prepare_themes_for_js )filterline 666 (+15 into the body)

    Filters theme data before it is prepared for JavaScript.

  2. apply_filters( wp_prepare_themes_for_js )filterline 816 (+165 into the body)

    Filters the themes prepared for JavaScript, for themes.php.

Uses · 23

Show all 23

Used by · 1

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_action

Changelog

Introduced in 3.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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-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.