Skip to content

Instantly share code, notes, and snippets.

@walterebert
Created October 27, 2015 08:42
Show Gist options
  • Save walterebert/05dceac3ecbf180de600 to your computer and use it in GitHub Desktop.
Save walterebert/05dceac3ecbf180de600 to your computer and use it in GitHub Desktop.
WordPress snippet: DNS prefetch
<?php
/**
* Prefetch DNS for given domains
*
* @param array $domains List of domain names
*
* More info: https://css-tricks.com/prefetching-preloading-prebrowsing/
*/
function my_dns_prefetch( $domains = array() ) {
if ( ! $domains ) {
// Default list
$domains = array(
'0.gravatar.com',
'1.gravatar.com',
'fonts.googleapis.com', // Google Fonts
'ajax.googleapis.com', // Google CDN
);
}
foreach ( $domains as $domain ) {
echo '<link rel="dns-prefetch" href="//' . esc_attr( $domain ) . "\" />\n";
}
}
add_action( 'wp_head', 'my_dns_prefetch', 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment