wp_resource_hints │ filter-hook │ WP 4.6.0

Allows you to change the list of resource hint URLs that a browser should process in advance, such as preconnect and prefetch hints.

The filter can add, change, or remove URLs that the browser processes in advance to speed up loading.

It fires when wp_resource_hints() outputs <link rel="dns-prefetch|preconnect|prefetch|prerender"> tags.

Do not remove the entire wp_resource_hints filter; disable only the required resources.

Usage

add_filter( 'wp_resource_hints', 'wp_kama_resource_hints_filter', 10, 2 );

/**
 * Function for `wp_resource_hints` filter-hook.
 * 
 * @param array  $urls          Array of resources and their attributes, or URLs to print for
 *                              resource hints.
 * @param string $relation_type The relation type the URLs are printed for. One of 'dns-prefetch',
 *                              'preconnect', 'prefetch', or 'prerender'.
 *
 * @return array
 */
function wp_kama_resource_hints_filter( $urls, $relation_type ){
	// filter...
	return $urls;
}
$urls(array)

Array of resources and their attributes, or URLs to output as resource hints.

  • ...$0(array|string)
    Resource attribute array or URL string.

    • href(string)
      URL to include in the resource hints. Required.

    • as(string)
      How the browser should process the resource, such as script, style, image, or document.

    • crossorigin(string)
      CORS policy for the specified resource.

    • pr(float)
      Expected probability that the resource hint will be used.

    • type(string)
      Resource type, such as text/html or text/css.
$relation_type(string)

Relation type for which URLs are being output. One of:

  • dns-prefetch
  • preconnect
  • prefetch
  • prerender

Examples

#1 Add prefetch instructions for custom URLs (domains)

Use wp_resource_hints to explicitly add browser instructions for particular URLs or domains:

add_filter( 'wp_resource_hints', 'wp_kama_resource_hints_filter', 10, 2 );

/**
 * Function for `wp_resource_hints` filter-hook.
 *
 * @param array  $urls          Array of resources and their attributes, or URLs to print for resource hints.
 * @param string $relation_type The relation type the URLs are printed for, e.g. 'preconnect' or 'prerender'.
 *
 * @return array
 */
function wp_kama_resource_hints_filter( $urls, $relation_type ){

	if( 'dns-prefetch' === $relation_type ){
		$urls[] = 'https://dnsprefetch.com/foo';
	}

	if( 'preconnect' === $relation_type ){
		$urls[] = 'https://preconnect.com/some';
	}

	if( 'prefetch' === $relation_type ){
		$urls[] = 'https://prefetch.com/some';
		$urls[] = '//prefetch-no-proto.com/some';
		$urls[] = [
			'href'        => 'https://prerender-array.com/some.css',
			'as'          => 'style',
			'crossorigin' => 'use-credentials',
			'pr'          => 12.5,
			'type'        => 'text/css',
		];
	}

	if( 'prerender' === $relation_type ){
		$urls[] = 'https://prerender.com/some';
	}

	return $urls;
}

The HTML <head> contains:

<link rel='dns-prefetch' href='//dnsprefetch.com' />

<link rel='preconnect' href='https://preconnect.com' />

<link rel='prefetch' href='https://prefetch.com/some' />
<link rel='prefetch' href='//prefetch-no-proto.com/some' />
<link href='https://prerender-array.com/some.css' as='style' crossorigin='use-credentials' pr='12.5' type='text/css' rel='prefetch' />

<link rel='prerender' href='https://prerender.com/some' />

#2 Remove a dns-prefetch rule from the HTML <head>

Suppose the HTML document's <head> contains an unnecessary dns-prefetch rule for the external.com domain that should be removed:

add_filter( 'wp_resource_hints', function( $urls ) {

	foreach ( $urls as $key => $url ) {
		if ( str_contains( $url, 'external.com' ) ) {
			unset( $urls[ $key ] );
		}
	}

	return $urls;
} );

Changelog

Since 4.6.0 Introduced.
Since 4.7.0 The $urls parameter accepts arrays of specific HTML attributes as its child elements.

Where the hook is called

wp_resource_hints()
wp_resource_hints
wp-includes/general-template.php 3907
$urls = apply_filters( 'wp_resource_hints', $urls, $relation_type );

Where the hook is used in WordPress

Usage not found.