So I’m trying to remove the canonical link in the header of WordPress for paginated pages but all the suggestions I tried aren’t working.
Here is my code which is in my functions.php file:
function my_add_noindex_tags(){
$paged = intval( get_query_var( 'paged' ) );
if(is_search() || is_404() ) :
echo '<meta name="robots" content="noindex,follow">';
endif;
if ($paged >= 2) :
add_filter( 'wpseo_canonical', '__return_false', 10, 1 );
remove_action('wp_head', 'rel_canonical');
echo '<meta name="robots" content="noindex,follow">';
endif;
}
add_action('wp_head','my_add_noindex_tags', 4 );
I know the code inside if ($paged >= 2) :
runs because this tag <meta name="robots" content="noindex,follow">
is in the head section.
Anyone know where I might be going wrong.
The issue here is the canonical link added by Yoast SEO aren’t properly removed as expected.
Cheers
4
Answers
After going through the Yoast SEO codes, it seems like the canonical action is added to
wpseo_head
action.You either have to use priority 1 when adding your function to run in
wp_head
to get this to execute properly, or do it with the appropriate method below ie. usingwpseo_head
action.For me only adding this to init action worked
All these answers didn’t help me and i couldn’t find the right answer for my scenario:
I had to set priority to higher than 10 because apparently Yoast changes the url at priority 10 so if you want to change their url you have to set a higher priority:
Change the canonical url with a custom function:
Delete the canonical url:
Be aware that if you do not set a priority it will use priority 10 as the default and it will not work.
I know I’m late to the party, but for the right implementation, you need to run ‘wpseo_canonical’ filter directly: