Remove Canonical URL from Single Post

#911979
  • Resolved Jen
    Rank Math free

    Hi,
    I am using Rank Math plugin and want to remove the canonical URL for single post with post ID 1234.
    I’ve got this filter that removes canonical url from ALL posts. I need to modify it for single post or few posts with post ID. Note: I’m also using Code Snippets plugin to insert php function.

    add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
    return false;
    });

    what I want to do is (for example)

    function rankmath_canonical_url( $canonical_url, $post ) {
    if ( $post->ID === 1234 ) {
    // remove canonical url on post id 1234
    ...

    Can you please help me to write the correct filter for removal of canonical url on single post and multiple posts (but not all posts).

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

    We apologize for the issue you are facing with removing the canonical URL for a single post in Rank Math. Thank you for raising this concern.

    To remove the canonical URL for a specific post or multiple posts, you can modify your filter as follows:

    add_filter( 'rank_math/frontend/canonical', function( $canonical_url, $post ) {
        // Check for specific post ID
        if ( in_array( $post->ID, array( 1234, 5678 ) ) ) { // Add more IDs as needed
            return false; // Remove canonical URL
        }
        return $canonical_url; // Return original canonical URL for other posts
    }, 10, 2 );

    In this code:

    • Replace 1234 and 5678 with the IDs of the posts for which you want to remove the canonical URL.
    • The in_array function allows you to specify multiple post IDs.

    This way, the canonical URL will be removed only for the specified posts while keeping it intact for others.

    If you have any further questions or need additional assistance, feel free to reply to this ticket. We’re here to help!

    Sources:
    How to Make Your Theme Rank Math Compatible » Rank Math

    Hello,

    It seems that you already marked this ticket as “Resolved”.

    Do you still need our assistance regarding this issue?

    Looking forward to helping you.

    Thank you.

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

You must be logged in to reply to this ticket.