Skip to main content

Howto: Integrate Facebook Open Graph With WordPress





Facebook Open Graph protocol allows you to share your blog content not only with your readers, but their Facebook friends as well. The best part is – whenever someone liked your content(s), it will be published on their Facebook profile. But that’s not all, Open Graph allows you to explore more interesting ways to interact and engage with your readers. Ultimately – if this is done right – it builds up your brand and increases your site’s traffic.
open graph protocol Howto: Integrate Facebook Open Graph With Wordpress
In today’s post, we are going to look into how to integrate Facebook Open Graph with a self-hosted WordPress in a detailed step-by-step guide. It will require editing your existing WordPress theme and creating a Facebook application (if you don’t have one).
Ready? Let’s fire up the browser and your favourite code editor. Full guide after jump.

Step 1: Create a facebook App

We’ll need an Application ID and to get that, you’ll need to create a Facebook App. If you already have a one, move on to step 2.
Creating an application is easy, here’s what you do:
  1. Logon to Facebook, go to the Developer’s page.

  2. Click "Set up a new app" button on the top right corner.

  3. Give a name to your new app, agree to Facebook terms, hit Create app.
    create fb app step 03 Howto: Integrate Facebook Open Graph With Wordpress

  4. Go to Web Site tab, fill up Site URL and Site Domain.

  5. Note down the value of Application ID somewhere and hit the "Save Changes" button.
    create fb app step 05 Howto: Integrate Facebook Open Graph With Wordpress

That’s all! You can always come back later to fill up the rest of the information.

Step 2: Replace Tag

Open up your theme’s header file (header.php) in your favorite editor. Always keep a backup copy just in case anything goes wrong.
Look for this following line of code, or one that starts with >
Replace it with:
Keep header.php open, we are going to need it for the 3rd step.

Step 3: Insert OG tags

Paste the following code right after  tag, or before  tag.
                   
Here are some of the the values you’ll need to alter:
  • Line 3: Replace your_fb_app_id with the Application ID from Step 1.

  • Line 4: You can get your_fb_admin_id under your Facebook Insights page, (More info). Click on the "Insight for your website" green button, grab the entire string of code and replace Line 4.

  • Line 12: This line determines the image that represents your post. If your theme supports WordPress Post Thumbnails, it should work fine. But if it doesn’t, it will fail gracefully without an image. Check out Step 3a for an alternative workaround.

  • Line 19: Replace logo.jpg with an url to your blog’s logo. It will be displayed when a non-post page on your blog is shared on Facebook.

Step 3a – When "wp_get_attachment_thumb_url" Fail

When wp_get_attachment_thumb_url() failed to work, you are likely going to a content attribute with no value, like what’s shown below:
A simple workaround will be to replace Line 12 with the following code:
Next, open up functions.php and insert the following code:
function catch_that_image() { 	global $post, $posts; 	$first_img = ''; 	ob_start(); 	ob_end_clean(); 	$output = preg_match_all('//i', $post->post_content, $matches); 	$first_img = $matches [1] [0]; 	if(empty($first_img){ 			//Defines a default image 			$first_img = "/images/default.jpg"; 		} 	return $first_img; }
This replacement code attempts to use a function call catch_that_image() to grab and output the URL of the first image it comes to encounter. Replace line 10 with URL to a default image if the function fails to find its first image.

Step 4: Insert Facebook Javascript SDK

The following Javascript gives you to access all of the features of the Graph API and Dialogs. It also allows you to integrate Facebook social plugins like the Like buttonFacepileRecommendations, etc with ease.
Place it in header.php, right after 
Replace your_fb_app_id in Line 4 with Application ID from Step 1 earlier.

Step 5: Let’s test it!

We are done integrating Facebook Open Graph to the WordPress blog. Let’s give it a couple of test to make sure we’ve done things correctly.

Test 1: View source code

Take a look at the source codes of one of the blog post, you should have something like this:
meta values Howto: Integrate Facebook Open Graph With Wordpress
Check the properties and its values, make sure they are correct.

Test 2: Install a Like Box

If you haven’t install a Facebook Like Button, then it’s probably time to get one. Place the following code anywhere (preferably before content or after content) inside single.php:
Next, get a friend to Like it. You should see something similar appearing in his Facebook profile:
facebook like in profile Howto: Integrate Facebook Open Graph With Wordpress

Comments

Popular posts from this blog

Hot and Sexy Big ass girls

There are very sexy and cute big ass girls images with very good resolution. visit here and get this nice hot collection with good resolution.    If you like to these images please comments here it is very important to us. Thank you for visiting here.

Upeksha Swarnamali

There are very large image collections of Sri Lankan famous Actress Upeksha Swarnamali with very good resolution. Visit here and download all hot images of Sri Lankan Actress & Models. Upeksha Swarnamali is a very famous actress in Sri Lanka and also She is very Hot & Cute. Now she has married.  And also She was elected as a member of the Sri Lanka Parliament in the 2010 General Elections held on 8 April 2010 representing the United National Party.There are large image collections for understanding her beauty with previous and nearly released images. Starting her carrier as a model, she appeared on several song videos including "Chanchala", and took a major role in the television drama series "PABA". She also participated in the reality dancing show Sirasa Dancing Stars, on 8 June 2008 she was eliminated. She gained national attention due to the controversial removal from the Paba teledrama aired by State run ITN, due to what she claimed as her open support ...

Shalani Tharaka (sirasa kumari)

There are large image collection of Sri Lankan very famous star Shalani Tharaka. These images only with very good resolution. You  can download these all images quickly now. Shalani Tharaka is a very famous upcoming young Actress and Model in Sri Lanka.She has won  Sirasa Kumariya Competition which was organized by Sirasa TV. After that all Sri Lankan known her as the Sirasa Kumariya.Sri Lankan youngsters are love this cute actress because of the beauty and romantic performance in Sinhalese Teledramas. Now She has married. She is now becoming the most famous Actress of Sri Lankan Teledrama industry. Her performance in all teledramas are very great and attractive. Because of her this popular She will be the most popular Actress in Sri Lanka as soon as possible. There are few images of Sirasa Kumariya winning time which was organized by Sirasa TV. If you want see all images of Sirasa Kumariya you can visit her face book account. Below Images have been taken when she was Acting. ...