Created
March 24, 2017 16:57
-
-
Save vivek-kumar-poddar/aa1ee95a66ef820f7a301dfc66f82aa8 to your computer and use it in GitHub Desktop.
Read more at: https://blogprime.com/enable-dns-prefetch-in-wordpress/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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