wppaste
WordPress

wp_resource_hints()

Since
4.6.0
Source
wp-includes/general-template.php:3614
Prints resource hints to browsers for pre-fetching, pre-rendering and pre-connecting to websites.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
10–11

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

Plugin surface
1 hook

Third-party callbacks on 'wp_resource_hints' 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_resource_hints() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always10–11wp_dependencies_unique_hosts()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11110–11212 fewer instructions than PHP 8.5
8.511310–1121
8.411310–11214 fewer instructions than PHP 8.3
8.311710–1121
8.211710–1121
8.111710–11212 fewer instructions than PHP 7.4
7.411910–1121

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:

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

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.

  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.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.