wppaste
WordPress

wp_default_packages_inline_scripts( WP_Scripts $scripts )

Since
5.0.0, 6.4.0
Source
wp-includes/script-loader.php:348
Adds inline scripts required for the WordPress JavaScript packages.

Compatibility

WordPress
since 6.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

$scriptsWP_Scripts
WP_Scripts object.

Performance profile

How much work a call to wp_default_packages_inline_scripts() 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

Reaches the database via get_user_by().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
243–262

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()one call below wp_default_packages_inline_scripts()
  • cacheobject cachewp_cache_get()one call below wp_default_packages_inline_scripts()
  • serializeserialisationmaybe_unserialize()one call below wp_default_packages_inline_scripts()
  • querycontent queryget_user_by()one call below wp_default_packages_inline_scripts()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_default_packages_inline_scripts() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
wp_installing() && empty($timezone_string)243–248get_rest_url(), sanitize_url(), ->add_inline_script(), wp_installing(), admin_url(), ->get_blog_prefix(), get_current_user_id(), get_user_meta(), wp_json_encode(), sprintf(), ->add_inline_script(), get_current_user_id(), ( function() {(), get_option(), get_option(), get_user_locale(), array_values(), array_values(), array_values(), array_values(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), get_option(), __(), get_option(), __(), get_option(), __(), __(), offset(), time(), ->add_inline_script(), ->add_inline_script()
!wp_installing() && empty($timezone_string)245–250get_rest_url(), sanitize_url(), ->add_inline_script(), wp_installing(), wp_create_nonce(), admin_url(), ->get_blog_prefix(), get_current_user_id(), get_user_meta(), wp_json_encode(), sprintf(), ->add_inline_script(), get_current_user_id(), ( function() {(), get_option(), get_option(), get_user_locale(), array_values(), array_values(), array_values(), array_values(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), get_option(), __(), get_option(), __(), get_option(), __(), __(), offset(), time(), ->add_inline_script(), ->add_inline_script()
wp_installing() && !empty($timezone_string)255–260get_rest_url(), sanitize_url(), ->add_inline_script(), wp_installing(), admin_url(), ->get_blog_prefix(), get_current_user_id(), get_user_meta(), wp_json_encode(), sprintf(), ->add_inline_script(), get_current_user_id(), ( function() {(), get_option(), ->add_inline_script(), ->add_inline_script(), ->format(), get_option(), get_user_locale(), array_values(), array_values(), array_values(), array_values(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), get_option(), __(), get_option(), __(), get_option(), __(), __(), offset(), time(), ->add_inline_script(), ->add_inline_script()
!wp_installing() && !empty($timezone_string)257–262get_rest_url(), sanitize_url(), ->add_inline_script(), wp_installing(), wp_create_nonce(), admin_url(), ->get_blog_prefix(), get_current_user_id(), get_user_meta(), wp_json_encode(), sprintf(), ->add_inline_script(), get_current_user_id(), ( function() {(), get_option(), ->add_inline_script(), ->add_inline_script(), ->format(), get_option(), get_user_locale(), array_values(), array_values(), array_values(), array_values(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), __(), get_option(), __(), get_option(), __(), get_option(), __(), __(), offset(), time(), ->add_inline_script(), ->add_inline_script()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev263243–26131 fewer instruction than PHP 8.5
8.5264243–2623
8.4264243–262313 fewer instructions than PHP 8.3
8.3277256–2753
8.2277256–2753
8.1277256–2753
7.4277256–2753

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 · 13

Show all 13
  • DateTimeZone::__construct()

Used by · 1

Source code

function wp_default_packages_inline_scripts( $scripts ) {	global $wp_locale, $wpdb; 	if ( isset( $scripts->registered['wp-api-fetch'] ) ) {		$scripts->registered['wp-api-fetch']->deps[] = 'wp-hooks';	}	$scripts->add_inline_script(		'wp-api-fetch',		sprintf(			'wp.apiFetch.use( wp.apiFetch.createRootURLMiddleware( "%s" ) );',			sanitize_url( get_rest_url() )		),		'after'	);	$scripts->add_inline_script(		'wp-api-fetch',		implode(			"\n",			array(				sprintf(					'wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );',					wp_installing() ? '' : wp_create_nonce( 'wp_rest' )				),				'wp.apiFetch.use( wp.apiFetch.nonceMiddleware );',				'wp.apiFetch.use( wp.apiFetch.mediaUploadMiddleware );',				sprintf(					'wp.apiFetch.nonceEndpoint = "%s";',					admin_url( 'admin-ajax.php?action=rest-nonce' )				),			)		),		'after'	); 	$meta_key     = $wpdb->get_blog_prefix() . 'persisted_preferences';	$user_id      = get_current_user_id();	$preload_data = get_user_meta( $user_id, $meta_key, true );	$scripts->add_inline_script(		'wp-preferences',		sprintf(			'( function() {				var serverData = %s;				var userId = "%d";				var persistenceLayer = wp.preferencesPersistence.__unstableCreatePersistenceLayer( serverData, userId );				var preferencesStore = wp.preferences.store;				wp.data.dispatch( preferencesStore ).setPersistenceLayer( persistenceLayer );			} ) ();',			wp_json_encode( $preload_data, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),			$user_id		)	); 	// Backwards compatibility - configure the old wp-data persistence system.	$scripts->add_inline_script(		'wp-data',		implode(			"\n",			array(				'( function() {',				'	var userId = ' . get_current_user_id() . ';',				'	var storageKey = "WP_DATA_USER_" + userId;',				'	wp.data',				'		.use( wp.data.plugins.persistence, { storageKey: storageKey } );',				'} )();',			)		)	); 	// Calculate the timezone abbr (EDT, PST) if possible.	$timezone_string = get_option( 'timezone_string', 'UTC' );	$timezone_abbr   = ''; 	if ( ! empty( $timezone_string ) ) {		$timezone_date = new DateTime( 'now', new DateTimeZone( $timezone_string ) );		$timezone_abbr = $timezone_date->format( 'T' );	} 	$gmt_offset = get_option( 'gmt_offset', 0 ); 	$scripts->add_inline_script(

Changelog

Introduced in 5.0.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.

6.4.0
Added relative time strings for the wp-date inline script output.from the docblock
5.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/script-loader.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.