Change OG:URL

#78138
  • Resolved Travel & Spa
    Rank Math free

    Hello,
    is there a way to manually change the meta property=”og:url” for specific pages/articles?

    I need to do it in order to redirect the Facebook crawler to the old domain to maintain likes and shares counts, according to Facebook best practices.

    Thank you!

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

    Thank you for contacting Rank Math today.

    You can customize the following filter and add it to your theme functions.php:

    
    /**
     * Allows the output of the canonical URL as OpenGraph URL, consolidating likes and shares.
     *
     * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/
     * @param $url Canonical URL
     */
    add_filter( 'rank_math/opengraph/url', function( $url ) {
    	return $url;
    });
    

    Hope that helps. If you have any further question(s), please let us know. Thank you.

    Thanks for the workaround, but actually I just want to change the OpenGraph URL of the old articles, without changing the Canonical URL.

    Let me explain better: we didn’t move from http to https, we have completely changed the domain. The old one has site-wide 301 redirects, so search engines (apart from Facebook for which we set an exception) must consider as canonical the new site and not the old one.
    Only the Facebook crawler (OG URL) should point to the old (whitelisted) domain to consolidate likes and shares.

    For this reason, we cannot just set the output of the canonical URL as OpenGraph URL…
    Wouldn’t it be possible to add the og:url field in the advanced controls of RankMath? That way only the Facebook crawler would consider the old article on the old domain as the main…

    Thank you!
    Kind regards

    Hello,

    Thank you for your valuable suggestion. I’ve added it to our internal suggestions lists. If it’s something that we’re able to introduce, we’ll be sure to let you know.

    For now, I would suggest you to add the og:url in the WP custom field and use the filter my colleague provided to change it on the frontend. Plase check the following example:

    
    add_filter( 'rank_math/opengraph/url', function( $url ) {
        $og_url = get_post_meta( get_the_ID(), 'post_meta_key', true ); // Change post_meta_key with your the meta key from your site.
        if ( ! $og_url ) {
            return $url;
        }
    
        return $og_url;
    });
    

    I hope that helps.

    Thank you!
    So, if I understand correctly, should I add the field name “post_meta_key” in the WP Custom Fields?
    And then add the snippet you suggested above to the functions.php file, to show the “post_meta_key” value instead of the standard content of the meta property=”og:url” in the frontend?
    is this correct?
    Sorry but I’m not an expert programmer!

    Alberto
    Rank Math business

    Hello,

    Well, you will need to create a field, and you will need to give that field a name (the meta key), for example: custom_og_input (The post_meta_key name was just a sample, equal that the custom_og_input name, you need to define your own one in the custom meta fields).

    That name should replace the post_meta_key in the code my colleagues shared, like this:

    add_filter( 'rank_math/opengraph/url', function( $url ) {
        $og_url = get_post_meta( get_the_ID(), 'custom_og_input', true ); // Change post_meta_key with your the meta key from your site.
        if ( ! $og_url ) {
            return $url;
        }
    
        return $og_url;
    });

    Looking forward to help you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

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

The ticket ‘Change OG:URL’ is closed to new replies.