wppaste
WordPress

wp_preload_resources()

Since
6.1.0
Source
wp-includes/general-template.php:3967
Prints resource preloads directives to browsers.

Description

Gives directive to browsers to preload specific resources that website will need very soon, this ensures that they are available earlier and are less likely to block the page's render. Preload directives should not be used for non-render-blocking elements, as then they would compete with the render-blocking ones, slowing down the render.

These performance improving indicators work by using <link rel="preload">.

Compatibility

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

Performance profile

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
8–15

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

Plugin surface
1 hook

Third-party callbacks on 'wp_preload_resources' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. 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_preload_resources() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8–15apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev998–15262 fewer instructions than PHP 8.5
8.51018–1526
8.41018–15264 fewer instructions than PHP 8.3
8.31058–1526
8.21058–1526
8.11058–15262 fewer instructions than PHP 7.4
7.41078–1526

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

One hook fires while wp_preload_resources() runs, in this order:

  1. apply_filters( wp_preload_resources )filterline 3992 (+25 into the body)

    Filters domains and URLs for resource preloads.

Uses · 3

Source code

function wp_preload_resources() {	/**	 * Filters domains and URLs for resource preloads.	 *	 * @since 6.1.0	 * @since 6.6.0 Added the `$fetchpriority` attribute.	 *	 * @param array  $preload_resources {	 *     Array of resources and their attributes, or URLs to print for resource preloads.	 *	 *     @type array ...$0 {	 *         Array of resource attributes.	 *	 *         @type string $href          URL to include in resource preloads. Required.	 *         @type string $as            How the browser should treat the resource	 *                                     (`script`, `style`, `image`, `document`, etc).	 *         @type string $crossorigin   Indicates the CORS policy of the specified resource.	 *         @type string $type          Type of the resource (`text/html`, `text/css`, etc).	 *         @type string $media         Accepts media types or media queries. Allows responsive preloading.	 *         @type string $imagesizes    Responsive source size to the source Set.	 *         @type string $imagesrcset   Responsive image sources to the source set.	 *         @type string $fetchpriority Fetchpriority value for the resource.	 *     }	 * }	 */	$preload_resources = apply_filters( 'wp_preload_resources', array() ); 	if ( ! is_array( $preload_resources ) ) {		return;	} 	$unique_resources = array(); 	// Parse the complete resource list and extract unique resources.	foreach ( $preload_resources as $resource ) {		if ( ! is_array( $resource ) ) {			continue;		} 		$attributes = $resource;		if ( isset( $resource['href'] ) ) {			$href = $resource['href'];			if ( isset( $unique_resources[ $href ] ) ) {				continue;			}			$unique_resources[ $href ] = $attributes;			// Media can use imagesrcset and not href.		} elseif ( ( 'image' === $resource['as'] ) &&			( isset( $resource['imagesrcset'] ) || isset( $resource['imagesizes'] ) )		) {			if ( isset( $unique_resources[ $resource['imagesrcset'] ] ) ) {				continue;			}			$unique_resources[ $resource['imagesrcset'] ] = $attributes;		} else {			continue;		}	} 	// Build and output the HTML for each unique resource.	foreach ( $unique_resources as $unique_resource ) {		$html = ''; 		foreach ( $unique_resource as $resource_key => $resource_value ) {			if ( ! is_scalar( $resource_value ) ) {				continue;			} 			// Ignore non-supported attributes.			$non_supported_attributes = array( 'as', 'crossorigin', 'href', 'imagesrcset', 'imagesizes', 'type', 'media', 'fetchpriority' );			if ( ! in_array( $resource_key, $non_supported_attributes, true ) && ! is_numeric( $resource_key ) ) {				continue;			} 			// imagesrcset only usable when preloading image, ignore otherwise.			if ( ( 'imagesrcset' === $resource_key ) && ( ! isset( $unique_resource['as'] ) || ( 'image' !== $unique_resource['as'] ) ) ) {				continue;			} 			// imagesizes only usable when preloading image and imagesrcset present, ignore otherwise.

Changelog

Introduced in 6.1.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 7.1.0 tag, from src/wp-includes/general-template.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.