Reputation: 1799
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
Upvotes: 1
Views: 3167
Reputation: 208
I know I'm late to the party, but for the right implementation, you need to run 'wpseo_canonical' filter directly:
function remove_canonical_pagination() {
$paged = intval( get_query_var( 'paged' ) );
if ($paged >= 2) {
return false;
}
}
add_action( 'wpseo_canonical', 'remove_canonical_pagination', 4);
Upvotes: 0
Reputation: 978
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:
add_filter( 'wpseo_canonical', 'change_canonical', 20 );
Delete the canonical url:
add_filter( 'wpseo_canonical', '__return_false', 20 );
Be aware that if you do not set a priority it will use priority 10 as the default and it will not work.
Upvotes: 0
Reputation: 1
For me only adding this to init action worked
add_action('init', function() {
add_filter( 'wpseo_canonical', '__return_false', 10 );
});
Upvotes: 0
Reputation: 2665
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. using wpseo_head
action.
function remove_canonical() {
$paged = intval( get_query_var( 'paged' ) );
if ($paged >= 2) {
add_filter( 'wpseo_canonical', '__return_false', 10 );
}
}
add_action( 'wpseo_head', 'remove_canonical', 4);
Upvotes: 1