wp_resource_hints()
- Since
- 4.6.0
- Source
wp-includes/general-template.php:3614
Description
Gives hints to browsers to prefetch specific pages or render them in the background, to perform DNS lookups or to begin the connection handshake (DNS, TCP, TLS) in the background.
These performance improving indicators work by using <link rel"…">.
Compatibility
- WordPress
- since 4.6.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_resource_hints() 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
- 10–11
- 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 113.
Third-party callbacks on 'wp_resource_hints' 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_resource_hints() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10–11 | wp_dependencies_unique_hosts() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 111 | 10–11 | 21 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 113 | 10–11 | 21 | |
| 8.4 | 113 | 10–11 | 21 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 117 | 10–11 | 21 | |
| 8.2 | 117 | 10–11 | 21 | |
| 8.1 | 117 | 10–11 | 21 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 119 | 10–11 | 21 |
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_resource_hints() runs, in this order:
- apply_filters( wp_resource_hints )filterline 3649 (+35 into the body)
Filters domains and URLs for resource hints of the given relation type.
Uses · 5
- wp_dependencies_unique_hosts()Retrieves a list of unique hosts of all enqueued scripts and styles.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- esc_url()Checks and cleans a URL.
- wp_parse_url()A wrapper for PHP's parse_url() function that handles consistency in the return values across PHP versions.
- esc_attr()Escaping for HTML attributes.
Source code
function wp_resource_hints() { $hints = array( 'dns-prefetch' => wp_dependencies_unique_hosts(), 'preconnect' => array(), 'prefetch' => array(), 'prerender' => array(), ); foreach ( $hints as $relation_type => $urls ) { $unique_urls = array(); /** * Filters domains and URLs for resource hints of the given relation type. * * @since 4.6.0 * @since 4.7.0 The `$urls` parameter accepts arrays of specific HTML attributes * as its child elements. * * @param array $urls { * Array of resources and their attributes, or URLs to print for resource hints. * * @type array|string ...$0 { * Array of resource attributes, or a URL string. * * @type string $href URL to include in resource hints. 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 float $pr Expected probability that the resource hint will be used. * @type string $type Type of the resource (`text/html`, `text/css`, etc). * } * } * @param string $relation_type The relation type the URLs are printed for. One of * 'dns-prefetch', 'preconnect', 'prefetch', or 'prerender'. */ $urls = apply_filters( 'wp_resource_hints', $urls, $relation_type ); foreach ( $urls as $key => $url ) { $atts = array(); if ( is_array( $url ) ) { if ( isset( $url['href'] ) ) { $atts = $url; $url = $url['href']; } else { continue; } } $url = esc_url( $url, array( 'http', 'https' ) ); if ( ! $url ) { continue; } if ( isset( $unique_urls[ $url ] ) ) { continue; } if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ), true ) ) { $parsed = wp_parse_url( $url ); if ( empty( $parsed['host'] ) ) { continue; } if ( 'preconnect' === $relation_type && ! empty( $parsed['scheme'] ) ) { $url = $parsed['scheme'] . '://' . $parsed['host']; } else { // Use protocol-relative URLs for dns-prefetch or if scheme is missing. $url = '//' . $parsed['host']; } } $atts['rel'] = $relation_type; $atts['href'] = $url; $unique_urls[ $url ] = $atts; }Changelog
Introduced in 4.6.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 6.8.8 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.