Trevin Chow

Technology and Photography

How to add Facebook’s Open Graph social plugins to your site

with 22 comments

I just finished my initial integration with Facebook’s Open Graph Social plugins for both this blog based on wordpress and my photo blog which is based on PixelPost.

Facebook’s documentation is definitely in it’s early stages because it’s very sparse in some areas, and lacks good cross-links to make the content easy to dig through.  Most of their documentation doesn’t even include a link to their Application center which is required to even get some of the social plugins to work since they require you to have an AppID.

Here’s a quick guide on what I did to get this running for the Like and Comments plugins for my photo blog. For wordpress, it was far easier since there are plugins for it.  I ended up using the FBLike plugin.

Create an AppID

Create an “application” for your site at http://developers.facebook.com/setup/.  Most of the settings can be left at their default, as we really only care about the AppID.

Asynchronously load the Facebook Javascript SDK

You have a couple of options for integration, using either iframes or their XFMBL based on their Javascript SDK. I opted for their javascript SDK as it gave more flexibility.  Just put this anywhere in your <body>.

<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: 'your app id', status: true, cookie: true,
xfbml: true});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>

The only change you have to make to the above is to insert your AppID where it says “your app id”.

Add the Like Button

The Like Button is definitely going to be the most popular of all the social plugins and is the one I started with first. You can use their code generator to spit out the XFMBL code you need or just use this format:

<fb:like href="http://your-page-goes-here.com" layout="standard" show_faces="true" width="450" action="like" colorscheme="light" />

The key part of this is to have “http://your-page-goes-here.com” match the actual URL that the Like button will appear on. This is the URL that is shared and should be unique on your site.  For my photoblog, I used built in functions in PixelPost  to generate the URLs dynamically (e.g. the token <SITE_URL><IMAGE_ID> results in http://www.trevinchowphotography.com/blog/index.php?showimages=88).  If you’re using WordPress, you can use the function get_permalink() if you opt not to use a plugin to generate the Like button.

The other options such as show_faces, and color scheme are described on the code generator page.  I couldn’t figure out how to right justify the text in the Like Plugin. That’s why the layout looks so hokey on myphoto blog with it not correctly aligned to the right of the images.

Comments Plugin

The comments plugin allows you to embed a comments widget on your pages to allow people to Like as well as add and view comments about the item.  The syntax is:

<fb:comments xid="<ID>" numposts="10" width="420" />

where xid the ID that uniquely identifies your page, numposts controls the # of comments a user can see in your widget, and width is, well, the width.

This ended up being the biggest pain in the butt because Facebook didn’t give complete information in their documentation. It seems that xid can only contains numbers, letters and underscores, despite their code generator taking in any character. What’s worse is that xid can be left blank and it defaults to the page’s current URL, so I’m assuming the FB Javascript SDK does some sort of encoding.

When xid is set to an invalid value, there is no visible error string/code thrown — the comment module just doesn’t display. You’re better off just omitting xid and leaving it to the default unless you have some specific reason to override it with your own unique ID.

Note: The comments plugin doesn’t have the same display customizations the Like button has. For example, there is no color scheme support. I ended up changing the background colors on the comments field to better accommodate their lighter colors UI.

Adding Open Graph properties to your pages

While it doesn’t seem like it’s required, Facebook’s documentation stats that you need to add meta data to your pages to “turn them into Open Graph objects”.  This is done  using <meta> tags.

The basic ones I included on every page are:

<meta property="og:site_name" content="Trevin Chow"/>

<meta property="og:locality" content="Seattle"/>

<meta property="og:region" content="WA"/>

<meta property="og:type" content="blog" />

<meta property="og:country-name" content="USA"/>

<meta property="fb:admins" content="trevin"/>

<meta property="fb:app_id" content="1234"/>

Change 1234 to your actual AppID, fb:admins is your Facebook username/ID and “blog” can be changed to any of their supported types.

There are a set of other Open Graph properties with values that will be based on the pages they are describing.

<meta property="og:title" content=" FOO"/>

<meta property="og:url" content="URL"/>

<meta property="og:image" content="IMAGE_URL"/>

The above property values need to be dynamically generated based on your content.  This blog is based on WordPress and I tried to find a plugin that would inject the properties for me, but I couldn’t find anything yet.  So I manually added this to the section of the header.php file:

<?php if ( is_single() ) {
echo '<meta property="og:title" content="' . get_the_title(get_the_ID()) . '" />';
echo '<meta property="og:url" content="' . get_permalink() . '" />';
}
?>

All the above code does is identify where it’s a single post page, then set the title and URL Open Graph properties to right values by calling into the wordpress functions.

For my photo blog, I added the “image” property, which is what is used for the content sharing back to facebook (instead of facebook guessing which image to use).  The code you’ll use will vary, but since I use PixelPost, this is what I ended up using:

<meta property="og:image" content="<SITE_URL>images/<IMAGE_NAME>"/>

That’s it!

That’s all you need to do to get up and running.  You’ll spend more time on formatting and integrating it into your site’s theme.  Facebook current offers 6 other social plugins that you can integrate as easily as the Like and Comments plugins.

Written by Trevin

April 25th, 2010 at 9:22 pm

  • http://twitter.com/simon_baptist simon_baptist

    Thanks for the post, very useful information. I did a simple install of the iFrame Like buttons last week & circling back now for a deeper integration.

    To do that, I'm going to try out this plugin: http://wordpress.org/extend/plugins/facebook-op…

  • http://firephil.org zachishi

    thanks that xid thing was really frustrating…..

  • http://www.silentkite.co.uk/drinkware/promotional-drinkware Promotional Drinkware

    I'm wondering if there is Like button for blog posts for blogger.Is there “Like” plug-in, add-on or widget for blog-spot? like face-book have? Thanks.

  • http://blog.bottomlessinc.com Bottomless

    Here's the WordPress plugin with FULL support for Open Graph:
    http://wordpress.org/extend/plugins/like
    No coding necessary, you can customize the look and placement of the button in the settings interface.

  • flightsdubai

    Thank you SO much. I was struggling for hours until I found this webpage.

  • Br Morfov

    I have one question. I have a like button with iframe and a fb:comments. They bot work ok. However, when I add a comment, in my wall i see the full title of the page, not the openGraph one I have set in the page. With iframe like button it works ok – it gets the og metas, but not with fb:comments.

    I have searched in the web, but did not see this problem anywhere

  • Ariel

    I can't no log wiht facebook on this site, why? is because open graph? please, chek it!

    Thanks!

  • Pingback: Facebook Social Plugins & Their Impact on Your Website » The Molstad Consulting Blog

  • http://trevinchow.com/blog Trevin

    Do you have a link to the page where this is happening that I can look at?

  • musikizme

    I'm trying to get the meta tags to show up, after setting a different custom “og:title” than the post title and it keeps showing the same original post title…h3lp!!!!!!!!!!!

    • lady_clare

      try parsing it using fb linter tool

  • RKRDO

    Hi, the tutorial is very good, but i've a question. Is possible obtain reports about the comments?. When i see my application on facebook, i've a option “DataStoreAdmin”, and it say that if I use data store API can store data, then How I use data store api to store my comments of comment box?.

    Thanks for you response

  • Jeff

    Thanks for the post, very helpful!

    I've implemeted this on my own Photoblog, yet when I click the “Like” button I can see that I like it, but I also see the word “Error” next to this? Any ideas? Am I missing something?

  • Jeff

    Ignore me! I hadn't even thought about clicking on the “Error”…pointed me in the right direction and now I have it sorted.

  • Monkey88

    So, how does this comment stream work/embed? Is Disqus better than Facebook's comments plugin?

  • Anonymous

    thank you!

  • Anonymous

    this was so helpful!

  • Soj

    Hi, I’m also using pixelpost (pixelution theme) and am trying with no luck yet to get the facebook to display the thumbnail of the post image. Pixelution utilizes prettyphoto which requires a rel value of “prettyphoto”. Wondering if this is a cause for my problem. I also have not created an app for the blog on FB. Do I really need to? TIA! Any thoughts welcome!

  • Blair

    Thanks for posting this. Your paragraph about the facebook comments saved me from shooting my dev :)

  • Raymond_jakubowski

    hi, great info.

    can i use open graph to work with my addthis plugin?

  • http://foreclosureclearance.com/las-vegas-foreclosures/ Las Vegas Foreclosures

    Great post mate. It really helps a lot.. Thanks!

  • http://foreclosureclearance.com/las-vegas-foreclosures/ Las Vegas Foreclosures

    Great post mate. It really helps a lot.. Thanks!

Get Adobe Flash playerPlugin by wpburn.com wordpress themes