WordPress 4.6 added support for Resource Hints, a W3C The World Wide Web Consortium (W3C) is an international community where Member organizations, a full-time staff, and the public work together to develop Web standards.https://www.w3.org/. specification that “defines the dns-prefetch
, preconnect
, prefetch
, and prerender
relationships of the HTML HyperText Markup Language. The semantic scripting language primarily used for outputting content in web browsers. 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.
With WordPress 4.7, you’re now able to pass specific HTML attributes to these resource hints to make even better use of them. Namely, the as
, crossorigin
, pr
, and type
attributes can now be defined when using the wp_resource_hints
filter Filters are one of the two types of Hooks https://codex.wordpress.org/Plugin_API/Hooks. They provide a way for functions to modify data of other functions. They are the counterpart to Actions. Unlike Actions, filters are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output.. See #38121 for more information.
Here’s an example of how one can use this new feature:
function makewp_example_resource_hints_attributes( $hints, $relation_type ) {
if ( 'prefetch' === $relation_type ) {
$hints[] = array(
'crossorigin' => 'use-credentials',
'as' => 'style',
'pr' => 0.5,
'href' => 'https://example.com/foo.css',
);
}
return $hints;
}
add_filter( 'wp_resource_hints', 'makewp_example_resource_hints_attributes', 10, 2 );
While crossorigin
can be used to set the CORS settings for a resource, pr
indicates the expected probability that the specified resource hint will be used. The official W3C specification has more information about these attributes.
Note: preload
is not yet supported by wp_resource_hints()
, mainly because of a lack of browser support and benefit for core Core is the set of software required to run WordPress. The Core Development Team builds WordPress.. This will continue to be reevaluated as browser support evolves for these emerging features.
#4-7, #dev-notes, #script-loader