Skip to content

Instantly share code, notes, and snippets.

@amboutwe
Last active July 7, 2023 19:54
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save amboutwe/66c583d2ef4015a8a244ee3e0e8cd1a0 to your computer and use it in GitHub Desktop.
Save amboutwe/66c583d2ef4015a8a244ee3e0e8cd1a0 to your computer and use it in GitHub Desktop.
Remove or modify the Yoast SEO prev or next URLs. Only copy the section of code you need.
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Change Yoast SEO Prev/Next URL on some pages
* Credit: Yoast Team
* Last Tested: Jun 10 2017 using Yoast SEO 4.9 on WordPress 4.8
*/
add_filter( 'wpseo_next_rel_link', 'custom_change_wpseo_next' );
add_filter( 'wpseo_prev_rel_link', 'custom_change_wpseo_prev' );
function custom_change_wpseo_next( $link ) {
/* Make the magic happen here
* Example below changes the rel=”next” link on your homepage
*/
if ( is_home() ) {
$new_link = 'https://example.com/custom-page/2/';
$link = '<link rel="next" href="'. $new_link .'" />' . PHP_EOL;
}
return $link;
}
function custom_change_wpseo_prev( $link ) {
/* Make the magic happen here */
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove Yoast SEO Prev/Next URL from all pages
* Credit: Yoast Team
* Last Tested: Jun 10 2017 using Yoast SEO 4.9 on WordPress 4.8
*/
add_filter( 'wpseo_next_rel_link', '__return_false' );
add_filter( 'wpseo_prev_rel_link', '__return_false' );
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove Yoast SEO Prev/Next URL on some pages
* Credit: Yoast Team
* Last Tested: Jun 10 2017 using Yoast SEO 4.9 on WordPress 4.8
*/
add_filter( 'wpseo_next_rel_link', 'custom_remove_wpseo_next' );
add_filter( 'wpseo_prev_rel_link', 'custom_remove_wpseo_prev' );
function custom_remove_wpseo_next( $link ) {
/* Make the magic happen here
* Example below removes the rel=”next” link on your homepage
*/
if ( is_home() ) {
return false;
} else {
return $link;
}
}
function custom_remove_wpseo_prev( $link ) {
// Make the magic happen here
}
@Farhanjoiya
Copy link

How can i use this code and where i put this code?

@tewfik-dj
Copy link

How can i use this code and where i put this code?

Either you put this in your functions.php file for your active theme, or you cherry pick code into your category page, or landing page, ...

@SarahHaruel
Copy link

Confirmed working

@aosipov
Copy link

aosipov commented Apr 15, 2021

well... since Yoast plugin rel="next" and rel="prev" doesn't work on the archive that uses page-template. I am looking for an answer here! Hope someone can help me! All works fine all my categories pages.
All my attempts to use code above failed... So i guess i am missing something.
Thanks
Alex

@fmurdeni
Copy link

Thank you

@ignacio-fm
Copy link

well... since Yoast plugin rel="next" and rel="prev" doesn't work on the archive that uses page-template. I am looking for an answer here! Hope someone can help me! All works fine all my categories pages. All my attempts to use code above failed... So i guess i am missing something. Thanks Alex

You found a solution with your issue? I have the same problem! My Yoast does not include self-referencing rel = "canonical" or "prev" or "next" to the page template pagination.

@Lammetje
Copy link

Hi i tried the "wpseo_next_rel_link" fix in functions.php but nothing is showing up in the header, is it still working with WP 6.2.2 and Yoast 20.9
i need this for a custom pagination ( paginate_links ) Thanks

@amboutwe
Copy link
Author

@Lammetje These snippets are to modify or remove the next/prev meta tags. Therefore, the next/prev tag must already be present in Yoast SEO output. It sounds like you're looking to add these as new tags, so the snippets will not work for your case.

Please note that Google says it doesn’t use rel=prev/next for pagination.

@Lammetje
Copy link

Lammetje commented Jun 20, 2023

Thank you @amboutwe for your quick response, let me question another way, my client need a a canonical link thats paged, its working fine on the archive pages ( https://lepaya.com/en/category/about-us/page/2/ ) but not on my custom template with use of paginate_links ( https://lepaya.com/en/articles/page/2/ ) i saw some other questions related to this and they pointed to the wpseo link, so that what i tried, thanks again for your time

@it-giants
Copy link

it-giants commented Jul 1, 2023

To be honest Yoast make a lot of things hard to accomplish, [competitor] make things way easy I am not advertising for [competitor] but I have suffered a lot for making the canonical URLs work and still cannot accomplish this, I have removed the custom made pagination and I used Built-in wordpress pagination by using this function the_posts_pagination but I still cannot see any canonical URLs in the category page (Archive page) from archive.php, I don't know what to do Yoast didn't explain that much about the topic....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment