How to remove default canonical URL in WordPress.
I'm using Yoast SEO and I tried with add_filter( 'wpseo_canonical', '__return_false' );
And also I tried with remove_action('wp_head', 'rel_canonical');
But not working.
How to remove default canonical URL in WordPress.
I'm using Yoast SEO and I tried with add_filter( 'wpseo_canonical', '__return_false' );
And also I tried with remove_action('wp_head', 'rel_canonical');
But not working.
Share Improve this question edited Jan 24, 2019 at 5:20 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Jan 24, 2019 at 5:18 Pavan VPavan V 131 silver badge6 bronze badges 2- How is it not working? Are you trying to cancel the default canonical redirects in WordPress? – Sally CJ Commented Jan 24, 2019 at 7:32
- Yes, I'm trying to give my own one. – Pavan V Commented Jan 24, 2019 at 9:15
1 Answer
Reset to default 2In WordPress, there's a function named redirect_canonical()
which basically:
Redirects incoming links to the proper URL based on the site URL.
And by default, the function is hooked to template_redirect
:
add_action( 'template_redirect', 'redirect_canonical' );
So you can cancel/disable the default canonical redirects in WordPress like so:
remove_action( 'template_redirect', 'redirect_canonical' );
However, if you're just trying to set a custom canonical redirect URL, then you can use the redirect_canonical
filter which is fired in the redirect_canonical()
function:
add_filter( 'redirect_canonical', function( $redirect_url, $requested_url ){
if ( put expression here ) {
// Do something with $redirect_url.
$redirect_url = 'custom URL here';
}
return $redirect_url;
}, 10, 2 );
But the Yoast SEO plugin is already filtering the canonical URL, so this should also work:
add_filter( 'wpseo_canonical', function( $redirect_url ){
if ( put expression here ) {
// Do something with $redirect_url.
$redirect_url = 'custom URL here';
}
return $redirect_url;
} );
But note that Yoast SEO doesn't pass the $requested_url
parameter; i.e. the original URL.