Skip to content

Instantly share code, notes, and snippets.

@vivek-kumar-poddar
Created March 24, 2017 16:57
Show Gist options
  • Save vivek-kumar-poddar/aa1ee95a66ef820f7a301dfc66f82aa8 to your computer and use it in GitHub Desktop.
Save vivek-kumar-poddar/aa1ee95a66ef820f7a301dfc66f82aa8 to your computer and use it in GitHub Desktop.
<?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