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&layout=standard&show_faces=true&width=450&action=like&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
{ 2 trackbacks }
{ 86 comments… read them below or add one }
← Previous Comments
Do you know how to get fb:like to work when you do not have a clean URL – like with struts?
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
Thank you… I did it. Add into my blog but did not register for fb.apps.
An excellent writeup. Fast and easy, it’s the best of all worlds! Thanks for providing this, made my day much easier.
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
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.
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
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
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
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.
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.
Using a PHP CMS but when input the code in my admin area the webpage shows the text of the code and not the “Like” button? Help!!
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.
how do i use a custom facebook or twitter icon/button to link to the page on my website?
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.
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
thank you for the tutorial..
← Previous Comments