Proper Canonical Tag help please

#510626
  • Resolved Hodo
    Rank Math free

    Hello there.
    Rank Math free. Store 15k products.
    At the end of construction. At this moment it is nonindex.

    Until the moment, I’m going to use RankMath pro, I would like a solution, please, to solve the part with the Canonical tag.
    I’m currently testing this:


    add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
    // change canonical URL for entire website
    return $canonical;
    });

    add_filter( "rank_math/frontend/next_rel_link", function( $link ) {
    // change the next rel link for entire website
    $link = '<link rel="next" href="'.get_next_posts_page_link().'" />';
    return $link;
    });

    add_filter( "rank_math/frontend/prev_rel_link", function( $link ) {
    // change the prev rel link for entire website
    $link = '<link rel="prev" href="'.get_previous_posts_page_link().'" />';
    return $link;
    });

    However, in tests done in online tools, some say I have canonical, others don’t.

    I’ve tried various things, but without ok results, like:


    // create the canonical meta tag for the current page
    function cano_add_canonical_link() {
    global $post;
    if( is_singular() ) {
    echo '<link rel="canonical" href="' . get_permalink( $post->ID ) . '" />';
    } else {
    echo '<link rel="canonical" href="' . get_pagenum_link() . '" />';
    }
    }
    // Add the function to the appropriate action hook
    add_action( 'wp_head', 'cano_add_canonical_link' );

    or


    // create the canonical meta tag for the current page
    function cano_add_canonical_link() {
    global $post;
    if( is_singular() ) {
    $permalink = get_permalink( $post->ID );
    } else {
    $permalink = get_pagenum_link();
    }
    //rank math's function to insert the canonical link
    if ( function_exists( 'rank_math' ) ) {
    rank_math()->schema->set_data( 'canonical_url', $permalink );
    if(!is_singular()){
    //Get prev link
    $prev_link = get_previous_posts_link();
    //Get next link
    $next_link = get_next_posts_link();
    //If prev link exist set it as prev link rel canonical
    if($prev_link) rank_math()->schema->set_data( 'prev_url', $prev_link );
    //If next link exist set it as next link rel canonical
    if($next_link) rank_math()->schema->set_data( 'next_url', $next_link );
    }
    }
    }
    // Add the function to the appropriate action hook
    add_action( 'wp', 'cano_add_canonical_link', 20 );

    Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for any inconvenience this issue may have caused you.

    Could you please share the affected URL so we can check and verify it as well?

    Meanwhile, Rank Math adds a self-referencing canonical URL by default when a page is set to index.

    If you’re referring to “Alternate Page with Proper Canonical Tag” error in your GSC, you can refer to this link:
    https://rankmath.com/kb/alternate-page-with-proper-canonical-tag/

    Looking forward to helping you.

    Thank you.

    Hodo
    Rank Math free

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Hello,

    The canonical links are only useful when your website is set to index and since your website is not live yet and is set to noindex, there’s no need to generate the canonical and so our plugin doesn’t do it.

    When you switch the website to the live version and set it to index the canonical will appear on the pages by default.

    Hope that helps and please don’t hesitate to let us know if you have any other questions.

    Thank you.

    Hodo
    Rank Math free

    Great.
    I understand.
    Thank you for answer.

    Hello,

    Glad that helped.

    Please feel free to reach out to us again in case you need any other assistance.

    We are here to help.

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)

The ticket ‘Proper Canonical Tag help please’ is closed to new replies.