detect custom canonical

#293779
  • Resolved Joe Klovance
    Rank Math free

    Hi,
    I am trying to detect whether the canonical that the ‘rank_math/frontend/canonical’ hook gets is a custom canonical or a default canonical. All that hook gets is a string. Is there any way to do that?

    Thanks,
    Joe

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

    Thanks for contacting us and sorry for any inconvenience that might have been caused due to that.

    The filter is used to remove or set a custom canonical URL for posts/pages. You will need to return the URL you want to set as canonical. Here is an example:

    add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
            $canonical = 'https://example.com/'
    	return $canonical;
    });

    Hope this helps. Let us know if you need any other assistance.

    No, that didn’t help at all as I already said I knew about that hook. Posting the code from the site where I found it is not helpful.

    What did help though was searching and finding this line of code:
    $custom_url = RankMath\Post::get_meta( ‘canonical_url’, get_the_ID() );
    That can be used to retrieve the custom canonical link that is put in through Rank Math. if $custom_url is empty that means the $canonical passed into the hook is a default.

    Hello,

    Thank you for the update.

    Glad that you were able to find the correct filter for your concern and we appreciate for sharing here.

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

    We are here to help.

    Thank you.

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

The ticket ‘detect custom canonical’ is closed to new replies.