Setup Filter to change OpenGraph URL ( og:url )

#277724
  • Hi,
    I am changing my whole website to https instead of http. (this is my site http://www.new-synapse.com/aps/wordpress/)
    This means my Facebook Comments and all share counts (FB, Pinterest etc.) will not load anymore(collected over 9 years).
    I found your page about Filters (https://rankmath.com/kb/filters-hooks-api-developer/).

    Filter to change OpenGraph URL ( og:url ).
    /**
    * 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;
    });

    Can you explain how I would get this to work?

    I understand how to create the functions file rank-math.php.

    But do I have to change the canonical url in all posts (95 posts and pages) to the http url first?

    Or would I create a list of all 95 post urls, and then add that add_filter code to the rank-math.php file for 95 lines with the urls?

    I read changing canonical urls for every url might have a problem – “However, this might not be ideal as the canonical URL wouldn’t output the correct reflection of it being HTTPS.”

    Also, I hope it would work with Elementor Pro and the share button plugin…

    FYI this is the official Facebook method: https://developers.facebook.com/docs/plugins/faqs#faq_1149655968420144

    Thank you!

Viewing 15 replies - 1 through 15 (of 24 total)
  • I’m curious if I upgraded to Pro would someone show me how to fill in this code?

    add_filter( ‘rank_math/opengraph/url’, function( $url ) {
    return $url;

    url 1 – has http? so http://www.new-synapse.com/aps/wordpress/
    url 2 – has https? https://www.new-synapse.com/aps/wordpress/
    url 3 – has http? http://www.new-synapse.com/aps/wordpress/?

    Can you specify that this difference (http vs https) applies to all posts and pages?
    Or does it need every page url and post url specified?
    I switched my sites from yoast to use this feature…

    Hello,

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

    When you will use the HTTPS version of your site, the HTTP version will simply be redirected to the HTTPS version. So you don’t have to change the canonical URLs or the og:URL tags. All the links coming to your site with the HTTP version will still work and show the HTTPS version.

    You can use the code given below in your .htaccess file(if you haven’t already) to redirect the HTTP version to the HTTPS version:

    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

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

    Thank you very much for your reply Jaideep. I can give that a try but what happens when somebody goes to the https url? How do the Facebook share accounts and comments know to populate that page from the http page? They populate based on the url will they know to read the url as http? I guess I will give this solution a try in the morning and see how it goes and report back.

    Hello,

    As my colleague explained, when migrating from HTTP to HTTPS via setting it up in your htaccess, the HTTP versions of your URLs will be redirected automatically to the HTTPS version and that will apply to your OG tags and canonical as well.

    Please do give it a try and let us know how it goes. If you encounter some issues with it, we are here to help you.

    There is a tool in my host Siteground that you use to change your website to https. So what I am understanding from you is that first I go into Siteground and use their settings to change my website to https. This is the instructions:
    ” Site Tools > Security > SSL Manager tool
    You can easily install one from your SIteTools > Security > SSL Manager, what will change is that the system will automatically add HTTPS in front of your domain.”
    Doing that breaks all Facebook Comments and Share counts, so after I do that, then, to fix FB Comments and share counts, I go into htaccess file and enter the code you provided?
    By doing that, when the site loads the https page, it will load the page code that was the page code for the http website, including the og:url instructing Facebook that this is the http og:url? But the url will stay https?
    I guess I will go ahead and try that and then see if it works and then report back to you guys.

    I’m not a developer but I can already tell this won’t work. Why would you guys have to write a filter to take care of this problem if just redirecting to https would work? And why would I need to redirect to https if my site is already loading https? (what I am asking for is for after it’s changed to load https not now loading http) When the site loads https that is when the problem exists so making it load https will create the problem that needs fixing. Do you see how none of the answers you gave make any sense?
    This other help ticket is asking the same thing and it’s a lot more complex:
    https://support.rankmath.com/ticket/change-ogurl/
    I think perhaps you should create a How To article to explain how to set up the og:url filter to work. That would be very helpful.

    Hello,

    If you want to keep your og:url as the http version of your site then please use the filter given below:

    add_filter( 'rank_math/opengraph/url', function( $url ) {
    	global $wp;
    	$url = 'http://new-synapse.com/'.$wp->request;
    	return $url;
    });

    It will set the keep the og:url as the HTTP version even on the HTTPS version of your site.

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

    Wow, thank you so much! I will try it right away. I was thinking maybe there was no solution in the world. Let me do it now and see how it goes.

    Hello,

    Let us know how that goes. Please make sure to clear the cache of your site after adding the filter.

    Looking forward to your update.

    Thanks.

    I flushed “Dynamic Cache” but I didn’t see Memcache and flush it not sure if that makes a difference – my host has 2 kinds of caching I guess

    Hello,

    We might need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress logins in the designated Sensitive Data section.

    Please do take a complete backup of your website before sharing the information with us.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Hello,

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

    Thank you.

    Note that I joined a wordpress monitoring company yesterday called WP tech support (https://wp-techsupport.com/) They haven’t done anything yet but I put in a ticket asking if they might investigate this issue because fixing errors is part of their wp monitoring service and I was at a loss for what to do. You can check the WP Activity Log (I just added it) to see if anyone else is logged in and if any changes have been made by myself or them – they are added as a contributor as WP Techsupport not sure if that comes up there. I created a backup of my site so if anything goes wrong that can just be loaded from siteground website. Thank you, I really appreciate it.

    That place doesn’t seem to be doing anything at all… if you can find a way for the Rank Math filter to work, I would be very grateful…

    Hello,

    I made some changes to the code and now the og:url of your posts/pages in HTTPS version is now being set as the HTTP version:

    Please check and let us know if you need any other assistance.

    Thanks.

Viewing 15 replies - 1 through 15 (of 24 total)

You must be logged in to reply to this ticket.