Skip to content

Instantly share code, notes, and snippets.

@jayshreehcl
Last active May 25, 2017 14:02
Show Gist options
  • Save jayshreehcl/fdc64f3caf45f86127b973cc036aa584 to your computer and use it in GitHub Desktop.
Save jayshreehcl/fdc64f3caf45f86127b973cc036aa584 to your computer and use it in GitHub Desktop.
Learn how to enable DNS Prefetch and DNS Prerender in WordPress. You can read complete tutorial at http://blogprime.com/dns-prefetch-prerender-in-wordpress-with-instant-articles-plugin
<?php
/* Please don't copy the opening <?php tag */
function blogprime_homepage_dns_prefetch() {
/* variable for storing the homepage url */
$homepage = get_home_url();
/* variable for storing the next post url */
$next_post = get_next_post();
/* variable for storing the previous post url */
$prev_post = get_previous_post();
/* if homepage is present (not empty) then do following */
if ( !empty( $homepage ) ) {
echo '<link rel="prefetch" href="'.$homepage.'" />
<link rel="prerender" href="'.$homepage.'" />';
}
/* if next post is present then do following */
if ( !empty( $next_post ) ) {
echo '<link rel="prefetch" href="'.get_permalink( $next_post->ID ).'" />
<link rel="prerender" href="'.get_permalink( $next_post->ID ).'" />';
}
/* if previous post is present then do following */
if ( !empty( $prev_post ) ) {
echo '<link rel="prefetch" href="'.get_permalink( $prev_post->ID ).'" />
<link rel="prerender" href="'.get_permalink( $prev_post->ID ).'" />';
}
}
add_action( 'wp_head', 'blogprime_homepage_dns_prefetch', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment