Preconnect to Third-Party Origins
Adds preconnect hints so the browser opens connections to external hosts (Google Fonts by default) earlier.
Opens WPIDE on
Code
add_filter( 'wp_resource_hints', function ( $urls, $relation_type ) {
if ( 'preconnect' !== $relation_type ) {
return $urls;
}
// Origins your pages load files from.
$origins = array( 'https://fonts.googleapis.com', 'https://fonts.gstatic.com' );
foreach ( $origins as $origin ) {
$urls[] = array( 'href' => $origin, 'crossorigin' => 'anonymous' );
}
return $urls;
}, 10, 2 );
Notes
Only list origins that every page actually uses.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.