Resource Hints in WordPress 4.6

Last updated on by admin

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 );

Leave a Reply