How to Add Facebook Like Button in WordPress

Posted on May 2nd, 2010 by in Tutorials | 149 Comments  
How to Add Facebook Like Button in WordPress

Facebook and Twitter are going head to head in their attempts for web domination. Twitter released their Anywhere Platform which allowed users to use twitter just about anywhere on the web. Now facebook has released their “Like” button. The Facebook like button extends their like feature to other sites on the web. If the users are logged into their facebook account, then they can “like” or “recommend” a web page to all of their facebook friends as they are reading it from the site. In this article, we will share the advantages of Facebook like button and show you how you can easily add it in your WordPress posts, blogs, and sites.

Note: Sometimes, Facebook doesn’t show the right thumbnail image, or the right title by default. To prevent that issue, you should add Facebook Open Graph Meta Data in your WordPress.

Advantages of Facebook Like Button

Facebook is world’s 2nd most visited site according to Alexa. Everyone from age 11 to 77 are on facebook. Each user has a news feed where they see updates from their friends. This news feed shows the link their friends like, people their friend added, pages their friends became a fan of and much more. Now if a user has 410 facebook friends, and he clicks “like” on your post, then it will be shown to all of his 410 friends. This is the best free word of mouth advertising you can get. But this does not end here. It gets better. Lets say, Amanda likes a link from one site, and David also click like on that same link. Muhammad who is a mutual friend of both David and Amanda will see a text like:

Your Friends Amanda and David liked WPBeginner’s Link: How to Add Facebook Like Button in WordPress

This is powerful because not only does this do an excellent job of Word of mouth advertising, but by mentioning two friends liking this post Muhammad is twice as likely to click like on your link. Now imagine this happening with 15 mutual friends and then increase all the way up to thousands of users. This like button can be the single most important factor in making your article go viral on facebook.

How to Add the Facebook Like Button in WordPress

Now that you know that this facebook button is very useful, lets add this in your WordPress post. First open your single.php file in your theme’s folder. Then paste the following code inside your post loop:

<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&amp;layout=standard&amp;show_faces=false&amp;
width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:60px;">
</iframe>

You can also use the XFBML method which we highly recommend because it allows you to receive further insights on your likes. Here is how to do it:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="<?php echo get_permalink(); ?>" show_faces="true" width="450"></fb:like>

Subscribe to our Newsletter, and see how you can get analytics from the Facebook Like Button.

Once you paste the code, you are done. You should see something like what we have at the top of our post. (Click on the button to see its functionality ;) ):

There are other parameters and options for this plugin.

If you want to show the friend icons under the button, then you should use the parameter:

show_face=true

If you want to display the simple button with only the count and nothing else, then you want to use the parameter:

layout=button_count

If you want a darker color scheme for your button, then you need to use the parameter:

colorscheme=dark

If you want to change the text from Like to Recommend, then use the parameter:

action=recommend

You can customize the width of the iFrame as you like.

If you have any questions, then feel free to ask in the comments. We also recommend that you look at our Smart Sharing Plugin to add a floating share box with retweet, facebook share, digg, and stumbleupon buttons.

Additional Resources

Facebook Documentation

About

Editorial Staff at WPBeginner mainly Syed and David.

Post comment as twitter logo facebook logo
Sort: Newest | Oldest
garrick1234 5 pts

Where does one find the single.php file?  Sorry I know that sounds pretty dumb, but I am (sometimes)

wpbeginner 60 pts moderator

 garrick1234 No such thing as a dumb question. You will find it in your FTP folder /wp-content/themes/yourthemename/Or in your WP-Admin go to Appearance > Editor and find it there.

How can you enable users to add their own comments to their recommendation at the time of posting?

wpbeginner 60 pts moderator

When the user clicks, a comment box should just open. Specially if you are using the FBML version.

thank you for the layout="button_count"!!! i've been looking for that everywhere!

tingmike 5 pts

Thanks. One problem that I have is that the button doesn't align with the Google+ and Pinterest buttons. It's just two pixels too low compared to the other buttons. Any idea how to make them align to the same horizontal alignment?

wpbeginner 60 pts moderator

 tingmike You can add CSS classes to make sure that it works.

Thanks this is brilliant!

Fran Dorf 5 pts

Where do I find the "post loop" in the single.php file? I've tried pasting your code in various places and nothing happens!!!

wpbeginner 60 pts moderator

 Fran Dorf You have to look at while have_post, the_post

While these instructions appear straightforward, they are of no use to someone who does not know how to "open your single.php file in your theme’s folder." While I am on the internet all the time, I have next to zero knowledge of html. I have added code before, but I need instructions on how to get to the code in the first place. I cannot find anything anywhere on wordpress about a "single.php" file. Please help. I really want readers to be able to share my posts on Facebook, Twitter, and through e-mail.

wpbeginner 60 pts moderator

If you are unaware of how to open your single.php file, then perhaps you should utilize one of the many free plugins that add a facebook like button to your site.

Hi, I posted the code on my site and it worked. However, the friend icon is not staying. When i refresh the page, the friend icon disappears. I want all of the friend that clicked the like button to remain so I would know who clicked the like button. I need their names for the prize drawing. Can you help? Here is my code:<iframe src="http://www.facebook.com/plugins/like.php?href=<http://tierneyomalley.com/?p=260?php echo urlencode(get_permalink($post->ID)); ?>&amp;layout=standard&amp;show_faces=true;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="20" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:60px"></iframe>

sorry. I tried posting my code but showed up as that WP beginner. My apologies

wpbeginner 60 pts moderator

your code is right... it seems like that its some sort of FB glitch that its not recording your likes.

Megan at Fiterature 5 pts

Hi - this is not working for me. I copy and pasted exactly what is above into my single.php section. I am using the Twenty Eleven theme. When I Update File and then check my website...no Like button. What am I missing?

papayacansada 5 pts

I've been having some headaches with this thing, all I want is a facebook like button on the homepage, I got that but the problem is that when the button is clicked it will like the most recent post and not the main page, after many attempts at the moment I have the code outside the loop, in the header so it displays pretty much everywhere, it seems that even this way when I'm on a single page the button work as supposed and likes go to the single post, my concern is the main page, I need it to like mydomain.com and not the latest post when I'm on the homepage.

wpbeginner 60 pts moderator

papayacansada where it says href in the code. Replace the php code with your site domain.

jenniferdoser 5 pts

This worked great - thanks! I'm just having 3 issues. (1) When I like a post, it shows the following text next to the thumbnail:You may use these HTML tags and attributes: <​a href="" title=""> <​abbr title=""> <​acronym title=""> <​b> <​blockquote cite=""> <​cite> <​code> <​del datetime=""> <​em> <​i> <​q cite=""> <​strike> <​strong>(2) I would like to have the comment box drop down when someone clicks the like button, as it does on this site and I can't seem to find any documentation on that.

(3) I tried to access insights after installing the Open Graph plugin, but I get this error message when I try to add insights for the blog:No admin data found at root webpage http://www.firelilyphotography.com/. Insights requires admin data at this root webpage for the specified URL www.firelilyphotography.com/blogWhat admin data do I have to add for insights to work?

Thank you!

wpbeginner 60 pts moderator

jenniferdoser Ok first, you need to follow the XFBML way if you want insights, and the dropdown message box on the like buttons.Then follow this tutorial:http://www.wpbeginner.com/wp-tutorials/how-to-get-facebook-insights-for-your-wordpress-site/

jenniferdoser 5 pts

I'm using the XFBML way and I'm still not quite there. I think the main issue is that when I first activated the plugin, I used my business page Facebook ID instead of my personal ID. I've since updated the ID, but I still get an error when I like a post:The app ID "73464248836" specified within the "fb:app_id" meta tag was invalid.This is my business page ID. I tried deleting the plugin and starting from scratch, but that's not working. How can I get WP to recognize the new ID?

RaunakSen 5 pts

Any one help me out the facebook like box is not showing the members thumbnails properly at my blog: http://www.psdblast.com, some times it's showing properly but mostly it's showing only 4 thumbnails.

I will be great full if any one can help me.

Thanks
Andy

kellydiek 6 pts

Are you aware of a plugin or even the possibility for this... you have a blog post with the like button on it. Currently has 15 'likes'. Let's say you edit your blog post with updated information. Can you force facebook to repost the blog link to the wall of the users who already 'liked' the post previously? (So they know it's been updated). Hope that makes sense. Requested by a client today and I've never heard of such a thing!

robolist 5 pts

Fantastic tutorial guys... Just like all of your tutorials throughout this website, they are all very easy to follow. Great stuff :)

I have been developing a new blog for some time now and am working on a customised theme. We have placed several social share buttons on each post very similar to how you have suggested here. I chose to do it manually rather than using a plugin because i dont seem to be able to get exactly what i want from the plugins whether it be the correct share buttons i want or the lack of styling options with the plugin.

Placing the buttons manually allow me to use what i want and style them how i want. The only problem is, for the life of me i just cant figure out how to get the buttons to appear in the loop with the excerpt.

I would be extremely grateful if you could point me in the right direction.

Many thanks in advance.

wpbeginner 60 pts moderator

robolist Are you using a theme framework?? If not then it shouldn't be very hard. Place the code above inside your loop on the index.php or loop.php or your archive.php file...

robolist 5 pts

wpbeginner thanks for that :) For some reason i was thinking it wouldnt work. My college placed the codes for share buttons there before but when clicked they would 'like' all of the posts together. Maybe he didnt use the get_permalink function. Although i am sure i told him to use it. but it is working now so thank you so much...

filipstrbad 5 pts

when i add the code to single.php it only shows the like button on the new posts and not the old ones. can i add it to the old ones too?

JustinElia 5 pts

This may be a very stupid question. I am using like buttons on my site and have tried various plugins and everything,however, how does one link the facebook like button with your page on your posts? Example : I go to like a certain post on facebook on my site and it says "So and So liked www.bigbackpacker.com post about blah blah blah" It doesn't link to my facebook page for my blog. I hope this isn't too confusing and any help would be appreciated, thank you.

wpbeginner 60 pts moderator

JustinElia This tutorial is for adding the like button for each WordPress post... if you want it to link to your facebook page, then simply change the href to facebook.com/yourfanpage

ssfutrell 5 pts

I uploaded the facebook like button plugin. It is on the bottom of my posts. I want it at the top of all posts. How do I change it to be at the top of post. Can't figure that out. I hope that you haven't answered already. I need very plain instruction. Thanks.

seocatalysts 6 pts

Thanks for inform me about how to add like facebook button? and I did it but still My confusion is that how can i show that how many people like my blog? For this problem solve any script available....Any help would be appreciate.....

wpbeginner 60 pts moderator

seocatalysts Use the facebook like box plugin...

SeviGadeaGimenez 5 pts

I like to ask you how i can do to center the like button, i mean that i have the button at the left of the page but i'd like to have it on the center.

thanks for all, and forgive my poor english

debbie.mccormick 5 pts

HELP I just put this code in my custom PHP folder and now my blog is gone.

I get this message:

Parse error: syntax error, unexpected '&lt;' in /home/debmcc4/mccormickmadness.com/wp-content/themes/thesis_181/custom/custom_functions.php on line 118

I copied and pasted it right in so I can't see how I could have put anything in incorrect. Please help me!!

wpbeginner 60 pts moderator

debbie.mccormick You can only paste functions in your custom-functions.php files...

If you notice in our directions we suggested that you paste it in your single.php file. However, I understand that you are probably using a child theme for thesis that is why you don't have access to single.php file.

You need to run this code as a function. Or use a plugin.

kiendlam 5 pts

How exactly do you see who actually clicked on the Like button. It usually shows one or two names and then a number, but I can't seem to click anywhere to see who has liked the post. Can you help? Thanks.

wpbeginner 60 pts moderator

If you follow our article about how to add open graph meta data, then it asks you to link your facebook account with your site. Then you will go to:

http://facebook.com/insights << There you can add your website. If your account is listed as it should, then it will show you all sort of cool insights about your site.

anthony_garcia 18 pts

how do you adjust the picture associated with the post when the Like button is clicked? I have a Twitter icon on my sidebar that keeps getting picked up as the picture for the post. Example: http://www.anthonyjgarcia.com/social-music-engagement-turntablefm

using xfbml on self hosted wordpress. THANKS!

wpbeginner 60 pts

anthony_garcia Use the plugin that we recommend in this article. Facebook Open Graph Meta Data. That will fix the issue.

kaniayubu 5 pts

For some reason I have been unable to get the XFBML version of this code to work. The iframe version works perfectly but as you mentioned in your other post, the send button doesn't work in the iframe version. Could there be any additional steps to get the xfbml version of it working. I don't even see a Facebook like icon when I use the xfbml code. Any help would be greatly appreciated. Thanks.

artist74 5 pts

this may be simple to most people, but WHERE exactly inside the post loop do you place the code?? The exact point? Many thanks.

wpbeginner 60 pts

artist74 There is no EXACT point because it varies. The exact point is determined by where you want the code to show (for example before post / after the post / within the post etc). Once you decide that then you can also do further styling via CSS!

artist74 5 pts

Many thanks for your incredibly speedy reply. I had to play around a bit and now it's working for me (bottom of the post on my blog). :-)

Will this now be as default (so for every post, this is what I want) or do I need to add anything else?

Many thanks.

wpbeginner 60 pts

artist74 No need to do anything else. It will be like that for all your posts.

any words of advice about the XFBML version? I copied the code straight from the facebook developer page, need to know if there is anything to do?

You need to add the Open Graph Meta Data to the site. We have edited the article. Look for the NOTE.

Nice post...
How to add facebook like button in wordpress.com?
Thanks.

Please, Visit my blog. ^_^

Tweets about us: