How to Add Facebook’s Like Button & Social Plugins to your WordPress Blog or CMS

FacebookUPDATED: Nov 24, 2011

Since Facebook introduced the Open Graph protocol and Social PluginsSocial Plugins at the April 2010 f8 Developers Conference, they have evolved the way these plugins are implemented.

This tutorial has been updated to reflect these changes.

NOTE: This does involve modifying the PHP files for your WordPress installation. Make sure you save the originals in case anything goes amiss.

If you’re not comfortable editing your WordPress source files, a number of WordPress plugins for Open Graph are available to handle this for you.

Step 1: Get an App ID for your WordPress CMS or Blog

To make your WordPress website or blog an “object” in Facebook’s Open Graph, you first need to get an App ID for your site or blog.

Read my tutorial on getting an App ID for your website or blog.

After you have successfully set up your blog as an application “object” in Facebook’s Open Graph, you then need to add the appropriate <html> and Open Graph meta tags to your site pages.

Step 2: Add Open Graph Meta Tags to your WordPress Blog Header File

You’ll need to add the meta tags to your “header.php” file located in your theme’s folder (wp-content > themes > [your theme] > header.php).

You’ll need to add a couple new values to the <html> tag of your header:

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:og="http://ogp.me/ns#"
xmlns:fb="http://www.facebook.com/2008/fbml">

And then add the following meta tags (taken from our blog) to your header.php:


<meta property="fb:admins" content="YOUR-ADMIN-ID"/>
<meta property="fb:app_id" content="YOUR-APP-ID" />
<meta property="og:type" content="article" />
<?php if(is_home()) {
?><meta property="og:title" content="My Blog Name" /><?php
} else {
?><meta property="og:title" content="<?php the_title_attribute( $args ); ?>" /><?php
}
?>
<?php if (is_home())
echo '<meta property="og:url" content="YOUR-BLOG-URL" />';
else echo '<meta property="og:url" content="'.get_permalink().'" />';
?>

fb:admins

Replace YOUR-ADMIN-ID with your Facebook User ID. Read these instructions on how to find a Facebook user’s numeric Facebook ID.

fb:app_id

Replace YOUR-APP-ID with the Application ID assigned to your blog when you set it up on Facebook, as described in this article (referenced above).

og:title

For the “og:title” attribute, you’ll want this to return the title for each blog post. As you can see from the above example, this simply involves a bit of PHP code that pulls in the post’s title.

og:type

As you see above, I utilized a PHP conditional statement in header.php to have the <meta property=”og:type” content=”blog” /> tag for the home page and the <meta property=”og:type” content=”article” /> for individual posts. This comes by way of Otto who has an excellent blog on WordPress, as well as a Facebook Connect plugin for WordPress. And this approach is also supported in this GitHub post.

As for the implications, in terms of Insights and the display of Pages in your “Home > Ads and Pages” section of your personal profile, we’ll have to wait and see.

You’ll also need to add the <fb:like> XFBML to your index.php file for your theme, as well as the call to the JavaScript SDK. I added it as the first content after the “main-content” DIV. You will then see the Admin Page option next to the Like button.

og:url

I used some PHP to set the URL for our blog’s landing page to the blog URL set in the application (YOUR-APP-URL). On the individual pages or posts, it will fill in the Permalink for the page/post. In “single.php” (for individual blog posts) and “page.php” (for pages), we omit the “href” value in the “Like” button code (fb:like).

og:description

The “og:description” meta tag: If you omit this meta tag, the value will be the “description” meta tag for the post, and that is what will show up as the description on a user’s Facebook profile when they “Recommend” or “Like” your post. (We use the WordPress SEO plugin Headspace which gives you the ability to create custom title and meta tags for each post. Highly recommended!)

og:image

This is simply the full/absolute URL to the image you want to have appear when someone “Likes” your page and it’s added to their personal profile stream. We omit this meta tag so that it uses the lead image from the blog post.

The below image is from my profile after I “Liked” this blog post:
How a Facebook Like shows up on a User's Profile

The orange logo is the logo pulled in with the “og:image” meta tag (make sure you make this value the FULL URL to the image!).

The title of the post is linked in blue, and the description meta tag content is the description below the title. The “I like this post!” is the comment I added to the Like.

And that takes care of the meta tags. There are a number of additional social graph meta tags you might want to check out.

Step 3: Add the Like Button or other Social Plugins to your Individual WordPress Posts or Pages

First, as mentioned earlier, you must add a call to the JavaScript SDK, if you use the XFBML/HTML5 version of the social plugin (which I recommend).

You will add the below code right after the opening <body> tag, which is usually in “header.php”. In some WordPress themes, it may be in “single.php.” Yes, WordPress themes vary!

<div id="fb-root"></div><script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=YOUR-APP-ID";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

Adding the Like Button to your Individual WordPress Pages

Facebook generates the Social Plugins code for you, on its Social Plugins page.

You should choose one of the two flavors of XFBML, either HTML5 or XFBML. If you use the iFrame version, you won’t get user-interaction data via Insights, and users won’t be able to add a comment to the Like they make.

However, for users who can’t modify their WordPress code, either the iFrame method or the use of a WordPress plugin will be their only options.

The Social Plugins you can use on your blog are:

For each plugin you wish to add to your WordPress blog or CMS, simply visit the plugin page, add the parameters into the “configurator” (as Facebook calls it), then click the “Get Code” button.

Facebook now enforces your associating the plugin with an existing App ID that you admin. You will probably want to associate with the Facebook application you created for your site or blog to get an App ID, and your site should appear in the pull-down menu next to “This script uses the app ID of your app”.

Just add the code Facebook provides for your plugin where you want it to appear on your WordPress posts and/or pages. This will involve editing index.php and single.php.

To add a Social Plugin to each post (commonly the Like/Send buttons), you will need to edit the single.php file:

wp-content > themes > YOUR THEME DIRECTORY > single.php

For the HyperArts blog, I put the Like/Send Button code just before the <div class="entry"> ... </div>. (I created a new DIV to contain all the social buttons on our posts — Google+, LinkedIn Share, Twitter Button and the Like/Send Button.)

Add the Like button to Pages by adding the above code for Posts — but with the below code for <fb:like> tag where the only difference is the PHP code for the “href” value — to the page.php file (wp-content > themes > YOUR THEME > page.php). For our blog’s theme, I put the code just before the closing tag of the <div class="entry"> ... </div>:

<fb:like action='like' colorscheme='light'
layout='standard' show_faces='false' width='650' send="true"/>

After saving your changes to the “single.php” file, you should be ready to go.

“Like” or “Recommend”?

For our blog, which contains more how-tos and tutorials than editorial content, I originally used the “Recommend” button, but decided that users were more familiar with the “Like” so I settled on “Like.”

Further Resources

Technorati Tags: , , , , , , , , , ,

  • Pingback: 19 Ways to Connect Social Media & Email Marketing Together

  • Benny

    Does anyone have any more/better info on how to “Programmatically administering your pages” than what can be found on http://developers.facebook.com/docs/opengraph ?

    I use fb:app_id and basically want to know when something is ‘liked’ on my site. There are several thousand pages that uses the like button so using fb:admins is not ideal. One would really like to get insights on domain level rather than on a per-page level.

    Maybe I’m searching for this? http://developers.facebook.com/docs/api (See the ‘Analytics’ section at the bottom of the page.) Is there anyone that has any experience using this?

  • http://www.g4m3.nl/ Niels

    If you use custom fields for images in your posts you can use below code to add the og:image meta tag to your header so the post image will be used when your readers share it on Facebook. This is much nicer than using for every post the same image.


    <meta property="og:image" content="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>"/>

    Replace the word thumbnail with the field name you’re using for your custom field. (check your posts in the admin panel of WordPress)

    It works on my own site. Thanks for the other info BTW, very useful!

  • http://elabee.co.tv eL Abee

    do you have any idea how to use fbml and validate w3 validator?

    • http://www.hyperarts.com timware

      I don’t believe w3 has an FBML validator and I seriously doubt a Facebook fan page would validate.

  • http://baghdadpics.co.cc Rozhbaiany

    I want to add like button to FBML tab on my facebook page
    is that possible?

    • http://www.hyperarts.com timware

      Nope. You can show the image of the link button anywhere you want, but the actual clickable link button allowed is Facebook’s at the top of the page.

  • http://blog.tricityhometeam.com/ Aaron Johnson

    When I fill in the information to “Create an Application” and hit the “create application” button, a blank, white page is the result. Nothing to fill in. I’ve done this numerous times and even added a / mark at the end of my blog’s domain. I use the full address including the http:// and nothing. My blog runs on WP and I’ve updated the version.
    What up?

  • http://www.editthesis.com/ fareed | editthesis

    thank you for a great guide its working

  • Pingback: Top Essential Wordpress Plugins | MLM Dream Saver

  • DJ

    Installed “fbog-ilike-button” plug-in using i-frame setting for buttons to appear on posts & pages and it shows up, but what about Admin options?

    Implemented FB Like button on other pages on website using Open-Graph meta tags & can “manage pages” through FB admin. Is it possible to add Open-graph tags on WordPress blog? If not on all posts, at least on pages … in order to see page in FB “manage pages”?

  • http://alexpriest.com Alex Priest

    Hi Tim!

    Thank you so much for the great guide–it’s been a huge help making it this far and familiarizing myself with the Facebook social plugins.

    That said, I’m having a few issues I was hoping you might be able to help me with, if you have time!

    First, I’m having issues getting the meta tags to work on my site. My goal is to have the “like” button on each post both on the front page of the site AND on each individual post page–then when users click the “like” button on the homepage, it “likes” only the article they clicked the button on. I’ve installed the Twitter button like this and it works perfectly–the “tweet” button tweets only the post that the button is associated with on the homepage.

    I can’t get this to work with the Facebook “like” button. I tried tweaking your line of php in the metadata in the header for the button to share the URL of the permalink (like I have for the Twitter button) but no matter what I try it always just “likes” my homepage, not the individual post.

    Any idea how to fix that? I’ve used the href="<? the_permalink() ?>" in the Facebook like button code for now as a workaround, but it would be nice to get it to work with the metadata.

    On the other end of things, I’ve installed the button on individual post pages using the same workaround (with the href), but it won’t give users the option to comment on the “like”. On the homepage, it pops up the window where users can add a comment, but on individual post pages it won’t. I took a look at how you’ve set it up in this post, for example, and here you can add the comment, but I can’t get it to work on my blog for some reason.

    FINALLY (sorry for the long comment and so many questions!), whenever someone “likes” one of my posts it shares both the post title and then adds on my site name at the end of it, which is redundant (since Facebook also adds “on alexpriest.com” to each “like”). In other words, when someone likes one of my posts, it shows up on Facebook as:

    ____ liked [post title] | alexpriest.com on alexpriest.com

    Anyway, once again sorry for the long post, but I would GREATLY appreciate any help/tips you could provide!

    Best,
    Alex

    • Wil

      Did you happen to find a solution here? I’m having the same issue

    • http://www.moneypress.com Richard Hartian

      I too am having similiar problems…however the photo that shows up on facebook has nothing to do with the individual blog post…any ideas on how to get the photo be the main photo of the blog post?

  • http://www.solmaldonado.com.ar/blog sol

    well, after reading alllllllllll about face like button in internet, I found this tutorial the best one, but…..in aplication facebook page nothings happens, it has error, those plugins also seems not to be working in my blog.
    So, because there are many people out there with working I like it button, my question is if they put it before wordpress 3.1 ? or if you know some new solution or some other way to implement this aplication in it post?
    Thanks for this great extensive tutorial, sol

  • http://blog.kineticpulse.co.uk Laura James

    Great post – much more helpful than the Facebook official guides.
    I’ve adapted it and created my own post about how to do it for Zen Cart if you’re interested

    http://blog.kineticpulse.co.uk/2010/08/add-facebook-like-button-to-individual.html

    (BTW I also had the blank screen problem after creating an application – but the app was there when I went to my applications home page in Facebook)

  • http://www.fastlanebusiness.com MJDeMarco

    Not sure if anyone else is having an issue creating an application and receive errors or blank pages, but in that case, FB creates the application but doesn’t give confirmation. I tried to create the APP about 12 times and when I was about to give up, I noticed that FB created the app 12 times! Anyhow, I deleted the extras and implemented this code which worked to PERFECTION. Thanks for the post … priceless!

  • http://eric-b.net Eric B

    Every one of these that I try winds up saying that people like my site, and not the post. Any ideas?

  • http://ucutathome.com/blog Dustin

    Where do I find my Admin ID? I do not know what this is.

    • http://www.hyperarts.com timware

      The Admin ID is the profile ID of the user who is a Page admin. If you have a vanity URL for your personal profile (eg http://www.facebook.com/timware), then go to this URL:

      https://graph.facebook.com/YOUR-VANITY-URL

      Or go to your personal profile and click on your avatar picture in the top left corner. Your ID # will be the string of numbers in the address bar of the browser after “id=”.

  • Neil Barlow

    How does one go about creating a “Like” button that unlocks material to people who click it. For Example: then band Matt & Kim make their new single available to anyone who “Likes” their fan page. The stream is locked though to anyone who has not “Liked” the page. How do you create this??

    • http://www.hyperarts.com timware

      Hi Neil. I don’t think you can do that with XFBML which is the FBML used on non-Facebook pages. You might consider doing this on your Fan Page, creating a custom tab with Static FBML app and using the Fans-only FBML: fb:visible-to-connection.

      Here’s our tutorial on using the Fans-only FBML.

      If you discover a way what you request CAN be done, please share it here. Cheers!

  • Neil Barlow

    Thank you very much for your response it was quite helpful!

    http://www.facebook.com/mattandkim

    I was wondering if you also had any idea how this page goes about doing what it is doing with the song stream they have on there. Unless you “Like” their page you cannot hear their new song. I was trying to implement that into my fan page

  • http://www.everparent.com Lee H

    I just wanted to thank you for your article and ALL the followup’s. I’ve had the Like button up and running before by scouring the net and getting info from various tutorials. I still didn’t have it running quite as expected. It would seem clear documentation isn’t easily found. But with your dedication to this post, you may well have the most complete (and human readable) tutorial I’ve seen. I was able to see my errors and was inspired to script all the variables making my like button 100% dynamic from post to post.
    Thank you so much :)
    Have a great day!

  • Belinda Harris

    Thank you! Great tutorial. It works great!

  • whoo

    timware, thank you VERY much for the pointer to the fb xml — I needed to retrieve the APP ID and only had the vanity url available and that solved it for me.

    thanks again!

  • http://www.Healingspirit.com Antonia

    Thanks for the post. I’m stuck on the instructions for Adding the Like Button to your Individual WordPress Pages

    The site says, “Add the Like button to Pages by adding the above code for Posts — but with the below code for tag where the only difference is the PHP code for the “href” value — to the page.php file…”

    I’m confused because 1) the fb:like tags are the same in both examples and 2) neither shows an href value, so I don’t know what I’m supposed to do here. Can you please clarify?

    Also, what if I want to add the button to one specific page, not every page?

    Thanks

  • http://www.hirekogolf.com Rob Altomonte

    I’m not sure you can help me with this but I have searched exhaustively for the code/answer to how to have a conditional Like button. I want customers to come to a facebook tab, then hit the LIKE button to receive a coupon code (not exclusive, I just want another jpeg to pop up with the coupon code after they hit LIKE). Must I only go through Wildfire to do this?
    Thank you!

    • http://www.hyperarts.com timware

      You need to use a Reveal Tab, where fans of your Page see one version of your Tab and non-fans see another. Read our tutorial on Reveal tabs to get started.

  • David

    I’m hoping someone may have a solution to why my og:image meta tag refuses to change anything. Here are the tags I currently have:


    <meta property="fb:app_id" content="APP_ID_HERE"/>
    <meta property="og:title" content="Show Us Your Shot Face"/>
    <meta property="og:type" content="Company"/>
    <meta property="og:url" content="http://www.website.com"/&gt;
    <meta property="og:image" content="http://www.website.com/small/14ca5ce825031c.png"/&gt;
    <meta property="og:site_name" content="website.com"/>

    I have tried appending random variables and values at the end of the url to trick the facebook caching, but i cannot get any change.

    The linter works fine of course, but all I can ever get is another image shown on the page. If there is any way to force a default image, or possibly even remove the picture so I can just use the comment box that would be fine.

    Thanks in advance!

  • Wil

    It seems that everything should be working perfectly to enable the “Like” button to refer to one post on my site. It’s just not happening – even with the custom php tweaks. Would love your help. Perhaps the problem is that I have a like button in the header and in each post – but, that’s exactly what I want.

    Here is the site w/ example post:
    http://www.foodoffmyblock.com/2010/11/01/roasted-tomato-sauce-2-ways/

    • http://www.kyleandrach.com Kyle Morgan

      Have you tried adding in the following code to pull the link permalink?
      <?php echo urlencode(get_permalink($post->ID)); ?>
      I had the same problem with trying to refer to just one post, and then have another for a universal ‘Like This Site’. I ended up downloading the plugin ‘Facebook Comments for WordPress’ which has a built in, well working Like button. And then I also did a Facebook App page for my site, in order to have a Universal Like button.

      http://www.facebook.com/apps/application.php?id=163335643684906

  • WC

    Everything works great for me… EXCEPT the image.

    If you click ‘Like’ it will show in your wall as just the title text (yes from the og meta data), but not the description or the image.

    BUT if you leave a comment when you click the ‘Like’ button, it works… ??? (title, image, description, website)

    Any ideas? I pretty much followed everything here. The URL Linter shows correct info.

    • WC

      Not sure what you guys are referencing but my page looks kind of like:

      … some stuff

      window.fbAsyncInit = function() {
      FB.init({appId: ‘MY_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);
      }());

      Feel free to tell me why only posting a “LIKE” with a COMMENT works, and a “LIKE” without a comment just uses the title and the link. (no image, no description).

      I would really appreciate any responses, questions, or tips.

      • Bret Perry

        That’s the way Like works, if the user shares a comment, the story gets more prominence, with text and thumbnail… if they do not share a comment, only title appears. It is working as designed!

  • http://www.baguje.com Sasa

    You have bug in your code. You’re calling og:title content=blog in first conditional tag instead of og:type

    • http://www.hyperarts.com timware

      Two different thing, Sasa. The above code sets the “og:title” attribute to “Blog” meaning that’s what I *want* the title to be if it’s the Blog home page. Users can substitute “Your Blog Name” or whatever. If it’s not the blog main page, it sets the og:title to the name of the post.

  • http://www.baguje.com Sasa

    Ok, but you know now now that’s a little bit confusing, because stype of page can be “blog” too. Thats only reason why i’d mixed things up. And thanks for this tut, i’m using it now on my blog, works great.

    • http://www.hyperarts.com timware

      Thanks for pointing out the confusing nature of the post. I’m going to correct it now to head off any further confusion. Much appreciated, Sasa.

  • http://whatgetsmehot.posterous.com cynophagie

    god, please redo this post for blogger and posterous (or if not specifically posterous, something that addresses a third-party host which does not utilize javascript, but definitely blogger would be appreciated and well-digested and trafficked).

    Thanks,

    as usual you guys are the only ones who get even close to addressing the ongoing nightmare that is Facebook’s application for blogs

    Cynophagie

  • http://dictionary.kaide.net indra kurniawan

    Thanks Tim for the easy instructions.

    I came here while trying to increase my site’s traffic via facebook (a bit late huh :) ).
    Before reading this I didn’t want to bother using XBFML; I was going to use IFRAME for fast result.

    Thanks again for showing how easy it is to setup facebook plugin using XBFML

  • http://nybodyart.com/ Danny Michel

    I tried this tutorial because the like button would only show up via iframe, and the other problem being the ‘like box’ on my sidebar not showing up anywhere but on the front page, but it doesn’t work. any ideas?

  • http://www.moneypress.com Richard Hartian

    Question, when using the code from facebook, the photo that shows up on facebook has nothing to do with the individual blog post…any ideas on how to get the photo to be the main photo of the blog post? I’m using wordpress and my site is http://www.moneypress.com…thanks

  • http://privacia.se/abstest Billy Bob

    Hmmm,
    A simple thing made very complex…
    Publicity is all about relations or is the other way atound?

  • Steve

    Hi,

    Great tutorial but having a very frustrating issue I just can’t seem to fix. Similar problem to what a couple of other people have posted here. When I press the ‘Like’ button, there is a few issues

    1) – All FB like buttons are ‘liked’ i.e. pressing like on 1 post will like all other posts. I can’t seem to de-couple the likes to liking only 1 article/post

    2) – Tied in with the above, liking a post will not send the post headline and image to facebook. Instead it sends the entire page.

    Now, from what I can see somehow the like button is pulling in my title, img and first p tag from my main site index.html page. I am not sure why, but that is what is getting sent to facebook. My index is in my root dir, so not sure how it pulls in that info from the /blog/wp-content/etc etc etc

    I have also tried using the popular plugin ‘Share & Follow’ to insert the buttons and just update the header.php but that does not work either. Inserting the buttons is very easy with that plugin and I would like to use it for flexability, but just can’t get the FB button to work right.

    Lastly, I built my own site and just use WP as the blog part. Not sure if that indicates something, but thought I would add it just in case.

    Any help appreciated… //Steve

    • Steve

      Sorry Missed a bit of info which may help.

      I checked the facebook linter and it seems that the meta tags I am not setting in the head are being pulled from the index.php file in my root (From my home page of my website)

      So maybe it is something to do with how the page treats meta data that is not set. According to this post it should choose the first image and description for the post. But it seems to do that but grabs the info from the wrong page.

      maybe that is more confusing! :-/

  • Rob

    Per the Facebook like page documentation, it is in fact possible for a user to add comments when the iFrame version of the button is used. Perhaps this functionality did not exist when the post was written?


    When will users have the option to add a comment to the like?

    If you are using the XFBML version of the Like button, users will always have the option to add a comment. If you are using the Iframe version of the button, users will have the option to comments if you are using the 'standard' layout with a width of at least 400 pixels. If users do add a comment, the story published back to Facebook is given more prominence.

    • http://www.hyperarts.com/blog timware

      Yes, the adding comments feature has been added to the iFrame Like button. I’ve updated this post to reflect that. Thanks!

  • http://ShaileshTripathi.com Shailesh Tripathi

    Your article on Facebook like button is very interesting and appealing to readers. http://shareitto.com thanks and wish to post you more article about it.

  • http://www.sintaposmaria.com/ Sinta Posmaria

    Thank you for your article. It makes me knowledgeable in facebook plugin a lot. Do you know why the face of facebook user especially when they are not in my friend’s list in my facebook, doesn’t show up in my facebook like button plugin and also my facebook like box plugin? I use my facebook like box plugin from my page in facebook (facebook plugin developers).

    Some time I try to change and use plugin from many plugin in WordPress, but still the result is the same. The image of facebook user doesn’t show up if they are not in my friend’s list. Why is it like that? I get frustrated to find from search engine to solve my problem. Do you know why is it like that? And how to fix it?

    Thanks for your kindness and attention. I’m sorry to trouble you.
    Thank you very much.

  • http://www.incrediblogger.net Make Money Online

    Oh thanks for this wonderful tutorial hyperarts.

  • http://procouponcode.com/ Franco

    Thanks for the article very much!
    Some times it’s not easy to find useful info at developer.facebook.com.

    For example, “Like Box” still offers not asynchronous loading //connect.facebook.net/en_US/all.js
    Thanks again that you describe everything very thoroughly!

  • http://www.facebook.com/lenny.sislac Lenny Sislac

    How about having clickable facebook button on the wordpress header image?

    • http://www.hyperarts.com/blog/ Tim Ware

      I’m not sure what you mean. If you want to link an image to a Facebook page or profile, that’s just simple hyperlinking. If you want to create a custom graphic for a Like button, you can’t.

  • http://pulse.yahoo.com/_TU3QECQAIQ5KCFXY4SQVCLROPE John

    Thank you so much for sharing this to us. Very informative and well-written.

  • http://www.hyperarts.com/blog/ Tim Ware

    Yes, Facebook requires that you use that namespace. I believe it’s needed for Facebook Connect to work with Internet Explorer. https://developers.facebook.com/blog/post/198/