0

Strange, _yoast_wpseo_meta-robots-noindex isn't a hook on hookr.io.

Do you know of a script to remove Yoast's output of rel=canonical on noindex pages?

1 Answer 1

1

Not sure if there are any plugins out there that do this. But a simple function to remove the canonical URL like below seems to work based on my tests.

function wpse282643()
{
    global $post;

    // Check if WPSEO plugin is active and bail immediately if not (this is just a sample check)
    if ( ! defined( 'WPSEO_VERSION' ) ) {
        return;
    }

    // Check if current page/post has noindex set
    $noindex = \WPSEO_Meta::get_value( 'meta-robots-noindex', $post->ID );
    if ( '1' === $noindex ) {
        // Remove canonical URL
        remove_action( 'wpseo_head', [ \WPSEO_Frontend::get_instance(), 'canonical' ], 20 );
    }
}

add_action( 'template_redirect', 'wpse282643', 99 );
2
  • Nice idea, that should work! I guess using wpseo_canonical will be more sustainable than working with yoast internals, though. From the docs: Allows changing of the canonical URL. Returning false will disable the canonical.
    – janh
    Commented Oct 12, 2017 at 4:59
  • Thank you, although I'm a little confused by function wpse282643(). Also, from Paul's comment, no use of wpseo_canonical ? Commented Oct 13, 2017 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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