14 February 2024

How to Connect Facebook Open Graph and WordPress

By Ronald Smith

With Facebook Open Graph, you can share your blog posts not only with your readers, but also with their Facebook friends. The exciting part is that whenever someone likes your content, it gets posted on their Facebook profile. But that’s not all – Open Graph offers even more engaging ways to connect with your readers. If done right, it can boost your brand and drive more traffic to your site.

In this post, I’ll show you how to integrate Facebook Open Graph with your self-hosted WordPress site. Don’t worry, I’ll guide you through each step in detail. All you need to do is edit your WordPress Themes and create a Facebook application if you don’t have one already.

Are you ready? Let’s get started by firing up your browser and opening your favorite code editor. I will guide you through the process.

Step 1. Creating a Facebook App

To begin, we need an Application ID, which requires creating a Facebook App. If you already have one, skip ahead to step 2.

Creating an application is simple. Here’s what you need to do:

  1. Login to your Facebook account and go to the Developer’s page.
  2. Click the “Set up a new app” button located in the top right corner.
  3. Give your new app a name, agree to Facebook’s terms, and click “Create app”.
  4. Visit the “Web Site” tab and enter the URL and domain for your site.
  5. Make a note of your “Application ID” and click the “Save Changes” button.

That’s it! You can always come back later to provide the rest of the information.

Now, let’s move on to the next step. To start, open your theme’s header file (header.php) in your favorite text editor. Make sure to create a backup copy before proceeding, just in case anything goes wrong.

In the header.php file, look for the following line of code, or one that starts with

Replace that line with:

Keep the header.php file open, as we’ll need it for the next step.

” />
” />
” />
” />
” />
/images/default.jpg” />

Make sure to modify the following values according to your needs:

– Line 3: Replace “your_fb_app_id” with the Application ID you obtained in Step 1.

In this step, we need to replace the image that represents our post. If our theme supports WordPress Post Thumbnails, we’re good to go. But if not, don’t worry, it will still work without an image. If you want an alternative, check out Step 3a.

Now, let’s move on to Line 19. Here, we need to replace “logo.jpg” with the URL of your blog’s logo. This logo will be displayed when someone shares a non-post page from your blog on Facebook.

Okay, let’s dive into Step 3a – what to do when “wp_get_attachment_thumb_url” doesn’t work. This usually happens when the content attribute has no value. Here’s an example:

Don’t worry, though. We have a simple workaround. Just replace Line 12 with the following code:



And then, open up the “functions.php” file and add this code:


add_filter( ‘wp_get_attachment_image_attributes’, function( $attr ) {

if ( empty( $attr[‘src’] ) ) {

$attr[‘src’] = ‘https://example.com/path-to-your-image.jpg’;


return $attr;

} );


That’s it! You’re all set now. Your post image and logo should be good to go.

In order to grab the URL of the first image it encounters, I created a function called catch_that_image(). This replacement code is designed to do just that. If the function fails to find the first image, you can replace line 10 with the URL to a default image.

Step 4. Insert Facebook Javascript SDK

By adding the following Javascript code to your website, you can access all the features of the Graph API and Dialogs. It also allows for the seamless integration of Facebook social plugins like the Like button, Facepile, Recommendations, and more.

Add this code to your header.php file, right after

In Line 4, replace the placeholder “your_fb_app_id” with your unique “Application ID” from Step 1. Now, let’s move on to Step 5 and put our creation to the test!

We’ve successfully integrated Facebook Open Graph into our WordPress blog. To ensure everything is working correctly, let’s run a couple of tests.

Test #1 – View source code:

Take a look at the source code of one of your blog posts. You should see something like this:

Check the properties and their values to make sure they are correct.

Test #2 – Install a Like Box:

If you haven’t installed a Facebook Like Button yet, now is a good time to do so. Add the following code anywhere in your “single.php” file, preferably before or after the content:

” layout=”button_count” show_faces=”false” width=”450″ action=”like” colorscheme=”light”>

Extra: WordPress Plugin

If you’re having trouble installing the codes or want a quick and easy solution, there’s a WordPress plugin available just for that.

The Facebook Open Graph Meta in WordPress plugin is designed to add Facebook meta data to your website. This helps prevent issues like missing thumbnails, incorrect titles, and wrong descriptions.

By installing the Facebook Open Graph Meta in WordPress plugin, you’ll ensure that your website displays correctly when shared on Facebook, avoiding any problems with how it appears in posts and links.