$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - How to remove canonical url in wordpress? add_filter( 'wpseo_canonical', '__return_false&|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - How to remove canonical url in wordpress? add_filter( 'wpseo_canonical', '__return_false&

matteradmin8PV0评论

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
Add a comment  | 

1 Answer 1

Reset to default 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.

Post a comment

comment list (0)

  1. No comments so far