Gary Bacon

Force Facebook Open Graph to Scrape on Publish in WordPress

On a recent project, the problem that we ran into is that on the first share Facebook has not scraped the content then it won’t pull the proper description or image for sharing.

Facebook debug tool allows you to test to see if Open Graph is being utilized properly for your content. The problem is many times once it has been “debugged” the API is just scraping the content so it has what it needs. You either have to wait until someone shares your content first, if that works well, or manually scrape it yourself.

Open Graph

Open Graph is great because it gives you control about how posts are shared in Facebook. Just a snippet of how I structure parts of my Open Graph tags in the theme:

<meta property="og:title" content="<?php the_title(); ?>">
<meta property="og:description" content="<?php echo blabla(); ?>">
<meta property="og:url" content="<?php the_permalink(); ?>">
<meta property="og:type" content="<?php
	if (is_single() || is_page()) { echo "article"; } else { echo "website";} ?>"
	/>
<meta property="og:site_name" content="<?php bloginfo('name'); ?>">

The Open Graph properties that I like to include are:

og:title
og:description
og:url
og:image
og:image:height
og:image:width
og:site_name
og:twitter

The Solution

We ended up creating our own functionality in functions.php that on publish will manually scrape the post with Facebook’s Open Graph API. Then once that is done, the WP site notes that it has been done so we won’t ping the service again.

As another example of this concept in use, Another WordPress Classifieds Plugin has a function that asks Facebook to scrape an Ad every time it is updated, created or enabled.

Hope that will help point others in the right direction!

Sources