1

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 is it not working? Are you trying to cancel the default canonical redirects in WordPress? – Sally CJ Jan 24 '19 at 7:32
  • Yes, I'm trying to give my own one. – Pavan V Jan 24 '19 at 9:15
2

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.