How to Add the Facebook Like Button to a Webpage




A couple of days ago FaceBook made some big announcements at the 2010 F8 developer conference.   The Universal Like Button was the announcement that has everyone most excited.  So let’s take a look at how to add a like button to any web page.

How to Add a Like Button to a Web Page

First the good news – It is terrible easy.   Now the bad news – FaceBook does a bad job of explaining it.  Ready for more good news?  I am going to simply things for you!

There are two ways to add the Like button to your Website – with an i-frame or with JavaScript.   We will discuss the difference and also take a look at some of the available WordPress Plugins.

Adding the Like Button to a Webpage using a simple i-frame

The easiest way to generate the i-frame code is to use the tool provided by FaceBook.  The attributes used in the i-frame code are straight forward however it’s best to make use of the tool because it automatically URL Encodes (converts special characters) you’re Web page URL for you.

The code below which was generated by the Facebook tool could be placed on http://lyndonreid.com/contact (note the colon (:) and forward slashes (/) are URL Encoded).

<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flyndonreid.com%2Fcontact&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:px"></iframe>

Adding the Like Button using XFBML and JavaScript

In order to use the XFBML you will need to reference the FaceBook JavaScript Library. You can simply  copy and paste the code that follows into the footer of your web page (or anywhere inbetween the opening and closing <body> tags) .  The code will load the JavaScript library from FaceBook at the same time as the rest of your site loads (asynchronously) meaning it will not block or slow the loading of the rest of your website.

<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>

Once you have the following code added to your webpage you can simply use the FaceBook tool to generate your XFBML or edit the code below and copy and paste it into your web page.

<fb:like href="http://lyndonreid.com/contact" layout="standard" show_faces="true" width="450" action="like" colorscheme="light" />

The benefit of using the XFBML is that users will have the option to comment when they “like” your page.  The i-frame version does not provide this functionality.

Adding the Like Button to a CMS

If you use the XFBML version of the code it’s quite simple.  You will load the JavaScript library on each page and then modify <fb:like /> tag to reference the current page.  Following are two examples, one that use PHP and one that uses VB.NET.

PHP

Note: this script assumes your site is running on http:// and not https://.
<fb:like href="<?php echo "http://" . $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI'];?>" layout="standard" show_faces="true" width="450" action="like" colorscheme="light" />
<fb:like href="http://lyndonreid.com/contact" layout="standard" show_faces="true" width="450" action="like" colorscheme="light" />

VB.NET

<fb:like href="<%=Request.UrlReferrer.ToString()%>" layout="standard" show_faces="true" width="450" action="like" colorscheme="light" />
<fb:like href="http://lyndonreid.com/contact" layout="standard" show_faces="true" width="450" action="like" colorscheme="light" />

Adding the Like Button to WordPress

There are two pluggins available already.  The plugin available at Kouguu.net allows for some customization via the control panel.

Smoking Gunn: http://blog.gunnjerkens.com/2010/04/facebook-like-plugin-for-wordpress/
Kouguu.net:  http://www.kouguu.net/?p=1

Adding the Like Button to a Thesis Themed WordPress Site

If you are familiar with the way hooks work within thesis then this will be really straight forward and you may not require this explanation.  If not, here you go J

Again, you can choose to do this with the XFBML of the i-frame version of the code.  I am going to show you how to do it the XBFML version as allowing you visitors to comments on posts they “like” is a nice touch.

We are going to add two hooks.  One for the actual button and one to load the required JavaScript.

You are going to edit you custom_functions.php file which can be found at:
\wordpress\wp-content\themes\thesis1.6\custom\custom_functions.php

Add the following lines to the top of the file:

add_action('thesis_hook_before_post','add_facebook_like_button');
add_action('thesis_hook_after_footer','add_XFBML_JavaScript');

Add the following to the bottom of the file:

//the following function will allow the facebook like button to display on every post
<div id="fb-root"></div>
function add_facebook_like_button() { ?>
<div style="width: 100%;">
<fb:like href=""<?php echo get_permalink($post->ID);?>" layout="standard" show_faces="true" width="450" action="like" colorscheme="light" />
</div>
<?php
}


//the following will load the FaceBookJ avascript Libraray
function add_XFBML_JavaScript() { ?>
<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><?php
}

The FaceBook like button will now appear at the top of every post in your Thesis themed WordPress blog.

Summary

Adding the FaceBook Like Button to a Web page is very simple. If you have any issues adding the button to your Web site leave a comment below and I reply with in few hours.  If you found this article useful, please scroll to the top and click the like link and tweet it :)

Comments

  1. says

    Excellent article.

    I’ve also pushed out a Facebook Like Button Plugin for WordPress the day before yesterday.

    It offers options you’ll find on the FB developers page … plus options to add the button to the top and/or bottom of your post.

    • says

      Thanks for stopping by Dean. Yeah i was going to add that but felt like I was going to on and on.

      Thesis peeps you can you the “thesis_hook_after_post” hook :)

  2. Gregg says

    Thanks for summarizing all this! You’ve saved me quite a bit of time next week. :)

    Here’s an easy change for that PHP code to support HTTP/S automagically:

    <fb:like href="” layout=”standard” show_faces=”true” width=”450″ action=”like” colorscheme=”light” />

  3. says

    I’m surprised you didn’t mention the Like Button exploit in that you can have it actually reference any page you want without the person realizing it while they think they are liking that particular page. :)

    Side note: Have you tried doing Thesis customization via the OpenHook plugin – MUCH better IMHO

    • says

      Hey Michael,

      Realized that after this post. Might have to do a series of posts.

      Haven’t checked it out yet, will do!

      Look at you droppin all the knowledge! Thanks man.

      Lyndizzle

  4. Lori says

    Thanks for the great info! I have the iframe version on my website and want to try out the XFBML version. What I’m not understanding in the script is this:

    FB.init({appId: ‘your app id’, status: true, cookie: true, xfbml: true});

    What’s “your app id”? I know that you can get something like this from the Facebook Developers page, but I’m not developing a custom FB app, I’m using one of their plugins. Do I just sign up for one and use the id it generates?

    • says

      I didn’t even notice that it was looking for an app id. You can generate one in the FaceBook developers area. I will get ya a link/more information :-) Thanks for spotting.

  5. says

    Thanks for posting this! I couldn’t get it to work within the confines of my CMS and didn’t have the patience to keep playing with it. But I did manage to be able to add the button manually to posts.

    Just as an FYI, the button wouldn’t show up for me until I wrapped the code in div tags, in case anyone else is having a problem with it not showing up. Not sure if that was a function of my particular CMS or not, so thought it was worth mentioning. I noticed on this site that yours was wrapped in divs so I tried that and it worked like a charm.

  6. Tiny says

    Lyndon: Your like button doesn’t show up at all from an IE8 browser. Bug? It doesn’t appear on any of your blog entries in IE8 but appears fine in Firefox.

    • Eric says

      Confirmed. I also don’t see your “like” button above if I use IE 8. It only works in Firefox. Weird, because I can see the like button in IE 8 for sites like CNN.com…

  7. says

    Lyndon, I think your vb.net has a small error.
    Request.UrlReferrer.ToString() in the VB.Net example should be Request.Url.ToString(). Otherwise your users will like the Referrer, which is not want you want.

  8. Bhavin Shah says

    Hi,

    I have added code of iFrame to display the Like Button but if i click on Like button, nothing will happen.
    Any solutions?

    Thanks and Regards,
    Bhavin Shah

  9. says

    I’ve added the iframe version of the button and it seems to work fine. I tried the XFBML version, but couldn’t get the comment box to work. I could see the top few lines of the box display, but not the entire box. I initially had this button within a table and then tried it within divs, but nothing allowed the full comment box to display. Has anyone else experienced this problem?

    Thanks!

  10. Bill says

    Hi,

    Similar to Lori, I have implemented the iframe version of the like button with no issues.

    But I can’t get the XFBML version to work. I’d like to use this mainly for the comments feature that comes with the Like.

    I’ve posted the code before and used the XFBML tag but no luck. I’ve also noticed the application id requirement in the code. I generated an id within facebook but it says to submit my application I need to have a certain number of fans???

    I don’t get it :-(

  11. says

    I do not understand what is so cool about this. I tried to add to my articles and tutorials on my web site and I get W3C validation errors. Or are there any workarounds that can solve the problem?

  12. Topher says

    Lyndon – Here is some sample code for you to try out

    Room It up

    Room It up

    window.fbAsyncInit = function() {
    FB.init({appId: ‘117030351648133’, status: true, cookie: true,
    xfbml: true});
    };
    (function() {
    var e = document.createElement(‘script’);
    e.type = ‘text/javascript';
    e.src = document.location.protocol +
    ‘//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById(‘fb-root’).appendChild(e);
    }());

  13. says

    I’m not a web developer or coder by any means, but have used dreamweaver to set up a site. I followed the directions from FB and have the “like” button on my webpage but it only shows my picture as “liking” the page and says “7 others” how can I get the “others” photos to appear? Also, how do I get it to show up on that persons profile when they “like” my site?? I’m very confused about this php something?? Can you give it to me in plain english please ;)

  14. says

    I’m starting to think this isn’t enough though. Most of us would be doing this in the hopes that the “like” will show up in people’s news feeds, but I’m not really seeing that happen much; it’s just showing up on profile pages.

    From what I can understand, we might need to also use the open graph protocol if we also want it to be featured in other people’s streams. From the like documentation page:

    “If your web pages represent profiles of real-world things — things like movies, sports teams, celebrities, and restaurants, you can optionally use the Open Graph protocol to enable users to establish lasting connections to your pages. Your pages show up in more places on Facebook and you gain the ability to publish stream stories to connected users.”

    Have I read that right?

    • says

      Hi Matt,

      I hope to give everything a good read and post some slick ideas this weekend :-).

      I’ll be back with more info :)

      Lyndon

  15. says

    Actually, the “like” button doesn’t work so well…. I am always getting an error (for all my sites). Same error the button on this very page returns lol (error is “page X could not be reached”). But it works on plenty of other sites.

    This is a symptom of some FB bug (#9923 on facebook bug tracker). Still I spent about 5 hours numbing myself with it.

    But thanks for the nice article anyway :)

  16. says

    hello,
    I have added code of iFrame to display the Like Button but if i click on Like button, nothing will happen. After clicking on link button it show error. Any solutions?

    Thanks and Regards,
    Renu

  17. says

    Hy, I have a very simple webpage, with just an embedded vimeo link with a short film i made. I want to post the new facebook like button so that my film gets more popular. This is the code that i embed using the facebook tool but i get an error. Can you please help find where am i wrong with this. Thanks a lot.

    my link : http://www.arpanjolly.com/bestideaever

    the facebook like code is as follows:

    Thanks in advance.

  18. says

    Hi.
    I am a beginner. Build my own website with antenna. Where must I copy and paste the fb like button code. I have tried with the Iframe, but i cant copy. What am i doing wrong

  19. Michelle says

    Exactly WHERE do I add this code????? what do I do after I have the code…..where do I click it onto my page at?? the post box, the link box wont take it

    WHERE do I do it at???

  20. Susan says

    Hi, great article. Does anyone know how to add like buttons to individual elements on the same page? I’m building a website for a band and want to display their videos on the same page and allow visitors to “like” individual videos.

  21. says

    This is a fantastic site. Is there a way to create a custom FB like button? I have a graphic I’ve built that I would like to use. Any information would be great. thank you.

    Greg

  22. says

    Thanks for the great post. I found just the right place to show me how to add the FB like button. Now I will get started on placing it on my page.

  23. says

    The site above is a new WordPress blog. I have not made any comments on it nor
    has anyone else. I’m a newbie at this that is why I’m asking.
    There are two things I’d like to accomplish.
    Add a LIKE button to the blog above and secondly,
    I like to add a LIKE button to my icontact newsletter. ( It has not been sent out yet)

    Frank

  24. Dusan says

    Is it possible to add a like button to a facebook page? I’m trying to find out how to do it but with little success.

    Thanks

  25. Milos Korac says

    Works great, added this at the beginning of the page:

    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);
    }());

    Then added this for button:
    <fb:like href="” layout=”standard” show_faces=”true” width=”450″ action=”like” colorscheme=”light” />

    Thank you ad Merry Christmas,
    Milos

  26. says

    I followed your suggestion to generate the Like Button code from the tool and pasted the code on my test page http://www.matiasmasso.es/test
    It woks Ok on Safari, but I get following errors on ie8:
    1) it does not recognize I am logged into facebook
    2) when I click the like button it pops up a blank page with a Facebook title on blue background from http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php?social_plugin=like&external_page_url=http%3A%2F%2Fwww.matiasmasso.es

    I’ve seen other pages with the like button working on ie8. I’ve pasted their codes on my page and they don’t work there on ie8 either.
    My webserver is Windows 2003 with IIS 6.0
    Any ideas?

  27. says

    I also added like button to site http://www.storymybaby.com/, but unfortunately in IE8 the button “like” doesn’t show. There is some errors in all.js code, but I’m not 100% sure if my code is properly defined. On the other side in Firefox and Chrome “like” button is visible.

    What’s wrong?

  28. says

    Please tell me why people’s pictures are not showing up underneath my like button:

    http://www.captainjackskeywestcharters.com

    Here is the code:

    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);
    }());

    Thanks.

  29. says

    I’m trying to add the button onto my website using the iframe code and nothing pops up. I use godaddy.com as my domain and page designer and I’m not sure if that affects it. The tech support staff at godaddy.com are close to useless. Thank you.

  30. katrina says

    I’m unable to get this to work :-(

    For now i’ve added the i-frame version to my page manually – though in th efuture i’d like to figure out if i can add it to the template of the page and have it automatically use that URL (may be too much for me to comprehend initially).

    The like button doesn’t even show up: http://tinyurl.com/nolikey

    Also, I’m confused by the width attribute… i only really want the small button, which is nowhere near the default 400 px that facebook offers by default.

    tanks in advance of any help/insight you can offer…

    katrina

  31. Mark says

    Hi. Thanks for the article. I was wondering whether it’s possible to also include a summary of the article when posting it back to FB.

    Thanks,

    Mark

  32. says

    Hi, I tried the code you suggested above and have some weird results. I am working in Joomla and am trying to add a LIKE Button only on sermons on my website (I also have a like box on the front page and that works fine). I added the following code to the component in Joomla:

    toString();
    echo $un; —– >>> for debugging to conform that I have a good URL
    ?>

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

    <fb:like href="” layout=”standard” show_faces=”true” width=”450″ action=”like” colorscheme=”light” />

    What I get is a LIKE notice on my facebook page, but all links point to the main website URL and not to the particular page from which I do the LIKE??
    Any suggestions will be much appreciated.
    Corky

  33. mary says

    Hello,
    Is there a way you could give provide a step by step instruction on how to do this in dreamweaver? I am new in this area and only using dreamweaver to create a website. I have tried several tutorial but to no avail. It would be a great help if you would reply asap.

    Thank you.

  34. says

    Hi Lyndon,

    I’m not a very accomplished web/soc network person but I thought I give it a try. I did as suggested but am not sure if I’m missing a trick.
    I get the button alright (using the Java version) but when I click on the button nothing happens….
    ??? Should I not get a ‘hand’ cursor at least?
    Any ideas?
    Thanks for your help
    Theo

  35. says

    Hi,
    I am not able to understand the instructions on how to add the like button on the website.
    Please help me with instructions.
    Its urgent!
    Thanks,
    Sharmila

  36. says

    Excellent article hosting here and Last few days i was found code of facebook like button and After Read entire post, I will try to implement this cod on my blog and Site as well..Thanks

  37. Dharnendra says

    Hi,
    If I want to pass the comment from my site to user’s facebook wall, how can I achieve that.
    I want to use some predefine site text which are also being posted to user’s wall, when user clicks on like button.

  38. says

    Can anyone please help me get my facebook like button onto my web page without me having to spend £100’s to a web designer….I have created a face book page for the web address but I cannot work out how to get the button onto my website. Please, if you can help email me. thank you so much.

  39. Rick Tatsch says

    Hi, FB really does a terrible job explaining how to add a Like Box. Reading the forums a lot of people are having the same difficulty. I know how to get the code but what is it excatly you do after you copy the code? At that point this is where everyone is getting stuck. Including me.

    Any help would be appreciated.

  40. says

    I am wanting to add the facebook like button to this website. When I have generated the code from FB developers and added it to my code, nothing happens. The website was made using a template so will this have something to do with why it won’t work? I didn’t make the site myself and don’t know a great deal about web design yet but my boss really wants this like button on the site! Please help if you can. Thank you.

  41. says

    HI,

    I’ve added the code from facebook developers website. My problem is that my button is not even showing in IE or CHROME
    Am I missing something ?

    Regards
    Dieter

  42. says

    Hi,

    Thanks for the help so far. I am not a web designer so i’m learning as I go.
    I have done everything needed on facebook to get the codes needed for my page but I am having problems putting it into the html code. I am using an old version of dreamweaver. I have created a box that I want the facebook like thing to go into but when i put in the code when i preview it it just shows the text of the code in that box. No facebook like panel.

    any help would be much appreciated!

  43. says

    I added a Like Box to my Info and Special offers page and It was supposed to be the dark color. It instead is clear any ideas as to how to fix this? I contacted my hosting company and they told me it was a scripting error with facebook. Yet I can not find any relevant info about fixing this or even contacting facebook to see if they could check the code. Any ideas?

  44. says

    I suppose I’m asking the same question as everyone else but perhaps you can dummy it down a little. I can see the Like button in Safari and Firefox but not Chrome. I saw it in Chrome when I initially added it but subsequently it did not show. This is what I have:

    (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″;
    fjs.parentNode.insertBefore(js, fjs);
    }(document, ‘script’, ‘facebook-jssdk’));

    Any suggestions? Thanks!

    • says

      That above code is not what I have nor is it what I copied/pasted to this post. Let’s try again.

      (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″;
      fjs.parentNode.insertBefore(js, fjs);
      }(document, ‘script’, ‘facebook-jssdk’));

  45. mohini says

    i am still not able to get the like button on my site….pls help me out.
    i hvae used php and javascript to dev it,

  46. sanny says

    Thanks it is very simple and i got it right well
    But there are already more 2,000 like even though i haven’t done it at all

  47. says

    Thank you so much I have been looking for simple instructions like this, you’re right facebook makes it sound so complicated!

    Many thanks again,
    Jason.

    • says

      Hi I am still facing problem when i am trying to use facebook like on my this Nehru Place website. I have used the full instruction as per this article but still missing some thing on actual. If any one can check on my site and help me…. Thanks a lot..!!!..

  48. Anurag says

    Is there a way to add face book like button but use our own like button, instead of what facebook provides.

  49. moatz says

    the following is part of php script associated with buttton ”

    var bootstrap = new bootstrap(‘http://hotmail.com’, ‘button’, ‘304770399584182’); bootstrap.init();
    the JS associated is here http://pastebin.com/1cunBBP0

    Using windows vista and chrome , I have facebook logged in , so why this line returns null in the JS ?

    if(document.getElementById(‘fb-root’)) return;

    I have tab in chrome already runing and using facebook , any suggestions

  50. says

    Well that really nice of you, that you have posted about like buttom. But my problem is how to get this particular code from facebook so that when a user clicks on like button it goes into the account of my site like i am running this nehru place market website and want to get facebook like code for this, how i can get it from facebook side… really confusing.

  51. sindhu says

    hi I want to display only fb like button. can you please tell me how to hide the comments which displays near like button “You and 220 others like this” .

  52. David Speight says

    OK, the Facebook tool gives me the button code, but you haven’t explained the ‘Step 2 – Get Open Graph Tags’ ?
    How about a series of step-by-step instructions, rather than an explanation of the differences and
    the theory of adding a like button?
    Can the process not be stated in a step -by-step fashion?

  53. maruifeng says

    I encountered a ‘confirm’ button when launching the like button. don’t know where it comes from. It says FB use this to prevent spamming or something.

  54. says

    hi, im trying to get this butoon to work but i see no button only a box that says internet explorer cannot display ths page. Does your facbook page have to be set up differently or settings changed or something? please help.

  55. Mary says

    No matter what I did button does not show on my page. Spent literally hours on it trying everything. But after reading FB dev forum it appears bug is there for 2 years. You know why? Because FB does not want free likes to your page. They want you to pay for each like. Forget about trying to make this work.

  56. Nibras says

    Hello. Is it necessary that when I add plugin it only works online? I am working on server and its not working out.

  57. says

    Sir, I have added the social plugin in my website default page and my problem is i want to send my own content and image when user clicks on like button.the following image and content show in users profile. please elaborate me with example. thank you

Trackbacks

Leave a Reply

Your email address will not be published. Required fields are marked *