Last active
January 15, 2022 07:16
-
-
Save Digiover/a84e879940adad46d29fc26ea32c8657 to your computer and use it in GitHub Desktop.
Prefetch and prerender WordPress posts with a function hook add_action
This file contains hidden or 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 | |
function saotn_post_prerender_prefetch() { | |
/* follow me on Twitter: @Jan_Reilink */ | |
$next_post = get_next_post(); | |
$prev_post = get_previous_post(); | |
if ( !empty( $next_post ) ) { | |
echo '<link rel="prefetch" href="'.get_permalink( $next_post->ID ).'" /> | |
<link rel="prerender" href="'.get_permalink( $next_post->ID ).'" />'; | |
} | |
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', 'saotn_post_prerender_prefetch', 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prefetch and prerender WordPress posts, load the next or previous post faster by prefetching and prerendering it in the background of your browser. More WordPress tips and usage in https://www.saotn.org/valuable-wordpress-snippets/
https://www.saotn.org/valuable-wordpress-snippets/#prefetch-prerender-wordpress-next-and-previous-posts