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.
In 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.