Resource Hints in WordPress 4.6

Resource Hints is a W3C specification that defines the dns-prefetch, preconnect, prefetch, and prerender relationships of the HTML Link Element (<link>). These can be used to assist the browser in the decision process of which origins it should connect to, and which resources it should fetch and preprocess to improve page performance.

In the upcoming WordPress 4.6, there will be a simple API to register and use resource hints.

By default, wp_resource_hints() prints hints for s.w.org (the WordPress.org CDN) and for all scripts and styles which are enqueued from external hosts.

Developers can use the wp_resource_hints filter to add custom domains and URLs for dns-prefetch, preconnect, prefetch or prerender. Here is how the filter works:

function my_custom_resource_hints( $hints, $relation_type )
{
if ( 'dns-prefetch' === $relation_type ) {
$hints[] = '//wphowto.net';
}
else if ( 'prerender' === $relation_type ) {
$hints[] = 'https://wphowto.net/sample-page';
}
return $hints;
}
add_filter( 'wp_resource_hints', 'my_custom_resource_hints', 10, 2 );
Disclosure: This page may contain affiliate links. If you follow one of those affiliate links and purchase something it will provide me with a little bit of a commission. This costs you nothing extra but helps maintain my site, free plugins, and themes. So I thank you for your support.

Leave a Comment