wp_preload_resources()
- Since
- 6.1.0
- Source
wp-includes/general-template.php:3967
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
- Scaling
- Scales with input
- Instructions
- 8–15
- Plugin surface
- 1 hook
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 101.
Third-party callbacks on 'wp_preload_resources' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8–15 | apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 99 | 8–15 | 26 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 101 | 8–15 | 26 | |
| 8.4 | 101 | 8–15 | 26 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 105 | 8–15 | 26 | |
| 8.2 | 105 | 8–15 | 26 | |
| 8.1 | 105 | 8–15 | 26 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 107 | 8–15 | 26 |
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:
- apply_filters( wp_preload_resources )filterline 3992 (+25 into the body)
Filters domains and URLs for resource preloads.
Uses · 3
- apply_filters()Calls the callback functions that have been added to a filter hook.
- esc_url()Checks and cleans a URL.
- esc_attr()Escaping for HTML attributes.
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.
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.