Preserve Facebook comments when changing URLs

It's not straightforward to preserve Facebook comments when changing URLs. Here's how to fix missing comments and engagements.

Share Post:

Share on facebook
Share on linkedin
Share on twitter
Share on email
preserve facebook comments when changing URLs

TL;DR : Changing URLs

Here’s a shortcut: It’s not straightforward to preserve Facebook comments when changing URLs. The HTML snippet for Facebook Comments that’s written to your page needs use the original URL of the page. The comments on the page are associated to that URL, even after your page URL has changed.

for example, if changing from https://host.com/path/to/old-slug/ to https://host.com/path/to/new-slug/

The data-href attribute still needs to contain the old URL:

<div class="fb-comments" data-href="<https://host.com/path/to/old-slug/>" data-width="100%" data-numposts="15"></div>

Your CMS probably generates the snippet, or create it manually here.

In WordPress or another CMS, the code is probably generated dynamically on each page. If you’re reading this, the code or plugin that displays the snippet probably didn’t support updating the URLs. You can hire me if you need a custom solution.

More about Facebook Social Plugins for comments and engagements

What you can do with Facebook Social Plugins

Visitors can comment, like & share your page with Facebook’s Social Plugins as they can with a page or post directly on Facebook. Naturally, this requires a visitor to have a Facebook login in order to use these features. Facebook stores your comments on their servers.

Where and how is this stored?

Facebook’s Graph API. Your page’s comments, shares, likes, and other meta are stored in an object that is keyed to the URL where the comments/shares/likes are embedded.

Here’s an example:

{
  "og_object": {
    "id": "3288267225520544",
    "title": "Comments Testing",
    "type": "website",
    "updated_time": "2021-05-12T17:37:08+0000",
    "engagement": {
      "count": 0,
      "social_sentence": "Be the first of your friends to like this."
    },
    "comments": {
      "data": [
        {
          "created_time": "2021-05-12T17:37:39+0000",
          "from": {
            "name": "Gabriel Herbert",
            "id": "10100023438570424"
          },
          "message": "Comment 3",
          "id": "3889487225120544_3882679381253780"
        }
      ]
    }
  },
  "id": "<https://host.com/2021/05/12/slug-of-old/>"
}

Use the following cURL request to query this object. Note that the page URL is directly in the URI. You can also query the object by its ID (3288267225520544 above), but that’d be a custom use case which isn’t supported by default in the SDK.

curl -i -X GET \\
 "<https://graph.facebook.com/v10.0/https://host.com/2024/05/12/slug-of-old/?fields=og_object%7Bid%2Cdescription%2Ctitle%2Ctype%2Cupdated_time%2Cengagement%2Ccomments%7D&access_token=API_TOKEN>"

How to associate the Open Graph object with a new URL

If your page URL changes and you’re only using comments, all you need is in the TL;DR. But likes and shares will not appear on the new URL (remember, they’re keyed to the old URL) without require additional work, as documented here and summarized in three steps:

  1. You must configure the site to return a 200 response for the old URL instead of 301/302 when Facebook’s crawler fetches it using the user agent string beginning with ‘facebookexternalhit’.
  2. The new URL must include these tags:
    <meta property=”og:url” content=”old-url” />
    <meta property=”og:type” content=”{matches original URL’s content}” />
  3. You must tell the Facebook scraper bot to index the new URL via the Graph API or their Sharing Debugger

Do these three steps to add the new URL as an ID to the open graph object where the comments and engagements (the shares/likes) are stored. I’m working on rolling this into a plugin.