22.4.10

Blogger: Add Facebook "Like" button to each post

Amit, over at Digital Inspiration, laid out some simple steps to getting the new FaceBook "Like" button onto your Blogger site. If only it had worked for me...

But, being a developer, I dug a bit into the Blogger templating engine and figured out how to make it work for real in a modern template. The snippet you want is:

<iframe 
   allowTransparency='true' 
   frameborder='0' 
   scrolling='no' 
   expr:src='"http://www.facebook.com/plugins/like.php?href=" 
             + data:post.url 
             + "&amp;layout=standard&amp;show_faces=true&amp;"
             + "width=530&amp;height=60&amp;action=like&amp;"
             + "colorscheme=light"' 
   style='border:none; overflow:hidden; width:530px; height:60px'/>

The "expr:src" tells the engine to interpret the XML attribute as an expression. The single quotes allow you to nest double quotes inside. The quoted sections are the parts that won't change, and the "data:post.url" is replaced with the fixed url for the given post.

As to where to put it, that really depends on your intention and current blogger template. In general, though, you need to edit your HTML template:

Layout Tab -> Edit HTML

Warning! Manually editing your template can hose your entire blog if done wrong. Be sure to back it up (instructions are on the Edit HTML page), and don't blame anyone but yourself if you mess it up!

In the edit box, search for "<b:includable id='post' var='post'>". That is the beginning of the post template. You should be able to make out the various parts of the post (comments, edit button, title, etc.) Try pasting the above snippet in various areas and then pressing the "Preview" button. (This lets you try it before you "buy" it ;) If you get a working "Like" button on each post, you are done!

Good Luck!

UPDATE 1 - No Line Breaks

In case you have issues, here is the version with all the clarifying formatting removed and no line breaks:

<iframe allowTransparency='true' expr:src='"http://www.facebook.com/plugins/like.php?href=" + data:post.url + "&amp;layout=standard&amp;show_faces=true&amp;width=530&amp;height=60&amp;action=like&amp;colorscheme=light"' frameborder='0' scrolling='no' style='border:none; overflow:hidden; width:530px; height:60px'/>

Note: When you paste this into the HTML editor, ensure that the snippet begins with "<iframe" and not "&lt;iframe". The snippet had to be escaped in order to display correctly in the HTML of this blog, but you need the real deal.

In addition, if you are using the blogger in draft template designer, be aware that there are some HTML editor errors that occur under certain conditions. If you are getting a mysterious error code, try switching to the draft HTML editor. (while on the Edit HTML page, replace "www" in the address bar with "draft") I had to change some attributes from having the value "" to having " " (a space) in order to work.

UPDATE 2 - String escapes

As reported by some readers, the previous drafts of this post were not correctly formatted. Inside the expr:src quoted strings, you need "&amp;" instead of "&". The above examples have been updated to allow for direct copy and paste.

49 comments:

  1. which version did you try and paste? The first friendly explanation one or the second, ugly one?

    Also, can you describe where you pasted the snippet? (i.e. between the "bleah" and the "bleargh" elements)

    Also, is this for the knuckle salad blog? Looks like a pretty customized template?

    ReplyDelete
  2. Alright. Sorry for the confusion. I have now successfully verified both snippets (full and compact) directly via copy and paste on several different blogs. I have updated the originals as well so there was no residual "wrong" version. Thanks for the feedback!

    ReplyDelete
  3. I actually figured it out. I think I was having trouble viewing the code and copying it correctly because a lot of pages are rendering the & quot;s and & amp;s even when I view them in plaintext. Could be a browser thing, I dunno. I ended up with this, but without the spaces and with <>s of course, and it works beautifully at last:

    iframe allowTransparency='true' expr:src='"http://www.facebook.com/plugins/like.php?href=& quot; + data:post.url + & quot;& amp;layout=standard& amp;show_faces=true& amp;width=450& amp;action=like& amp;colorscheme=light& quot;' frameborder='0' scrolling='no' style='border:none; overflow:hidden; width:450px; height:70px'

    ReplyDelete
  4. Thanks Buddy, worked like magic. Been breaking my head the whole day to crack this and your fix worked just fine. Thanks a ton.

    ReplyDelete
  5. Thanks you, this was really helpful :)
    I clicked your adds ;)

    ReplyDelete
  6. Many thanks!
    The plugin doesn't show the number of "likes". In Wordpress the plugin shows it...
    It's singular...
    Ernesto

    ReplyDelete
  7. Can't find where to paste the code--I don't have the <b:includable code...please advise...thanks.

    Barb
    www.halfpastkissintime.com

    ReplyDelete
  8. oops. found part of it after expanding widget template, but not the same. You would be my hero if you could help me do this!! :)

    ReplyDelete
  9. Thanks for the info!

    I have successfully installed the button, but it seems like the "Likes" count is incorrect. I have tried liking a post from several accounts, and it always appear as only 1 like.

    Any idea how to solve this?

    ReplyDelete
  10. Hmm...Just tried this on a personal site with both my and my wife's FB account. It was kinda buggy in my opinion, but I am pretty sure that is all on FaceBook's side and will improve. Here is what I tried:

    1. I clicked that I liked a post.
    2. It made me log in to facebook in a separate window.
    3. Refreshed the post, and the like went away.
    4. Clicked like again.
    5. Refreshed, and like remained.
    6. Went to the post on my wife's computer. Saw no likes.
    7. Logged my wife into facebook and refreshed the post.
    8. Saw my "like"
    9. Clicked "like" as my wife.
    10. Came to the page with a different browser (not logged in to facebook) and it appeared as if there were no likes and no like count.

    The documentation for the widget is here:
    http://developers.facebook.com/docs/guides/web#plugins

    Once you have the widget in your template, viewing source on the page should allow you to see the iframe rendered as the facebook documentation suggests. I am guessing that they are just working out some of the kinks?

    ReplyDelete
  11. @The Mad Katter - I think this is how the facebook side of this works, and I am not sure that I can do anything about it. I think further improvements would require more use of the facebook api.

    ReplyDelete
  12. Thanks so much. This is a lifesaver! Works great for me. I appreciate you taking the time to figure it out and then sharing too!

    ReplyDelete
  13. Thank you so much! You are a lifesaver!

    ReplyDelete
  14. Hi there. This code worked great but after a few days I got an error message, saying the specific page could not be reached, although it is. Any ideas?

    ReplyDelete
  15. Hi, I was able to add the button to my posts on my blog (www.giddyfingers.com). I can click it and it shows that I like it, but then it doesn't show this action on my actual facebook page on recent activites? Isn't it supposed to do that so that my friends on facebook can then see what I liked it? Thanks for your help! Marsy

    ReplyDelete
  16. Thanks a lot for this.. and it works just fine...

    ReplyDelete
  17. Thanks for the Facebook Like button tip. It works for me. But when i hover mouse over Like button, it shows You like "Blog Title" instead of You like Blog post title. Is there any way to customize this?

    ReplyDelete
  18. @7ths - I took a quick peek at your site, and it looked like it was correctly working. I am guessing this has to do with which tag you chose to put the code in. Try moving the code before and after sections in your template. That said, it does look like it is working...did you figure out your issue?

    ReplyDelete
  19. It's working fabulous and I really appreciate you posting this for all of us! I was actually considering manually adding a "like" button to every post as I went... What a time saver to have this available! Thanks again!

    ReplyDelete
  20. Worked great - thank you!!

    ReplyDelete
  21. i like, but only appear on my personal wall?

    how to make it appear on public as well?thanks

    ReplyDelete
  22. Thanks a lot . It works fine :)

    ReplyDelete
  23. I added like button but when I click like shows an error.

    The page at http://welearnfree.blogspot.com/2010/11/we-learn-free-new-look-on-learning.html;layout=standard;show_faces=true;width=530;height=30;action=like;colorscheme=light could not be reached.

    As far i understand, the prob is with placing frameborder='0' scrolling='no'...the closing or something is missing. Please help me

    ReplyDelete
  24. Does this work per post of for your entire blog. I have a 'Like' button with different code, but when someone 'likes' the post, it then shows up on every post. I want something that people can 'like' a post...then 'like' another post, etc.

    How does this code work in relation to that?

    ReplyDelete
  25. Wow, this worked so well.
    Thanks a lot.
    I've tried uploading the like button with the help of facebook itself but to no avail.
    Then your article came along. GREAT!

    ReplyDelete
  26. oh.. superb... i installed that button successfully

    ReplyDelete
  27. This is making me nuts - none of the things you said to look for in my template are there...I tried pasting each of your code options into a bunch of diff places (one at a time) but none make that darned like button show up. Help?
    http://www.owhstarsandstamps.org

    ReplyDelete
  28. worked like a charm! awesome! thanks!

    ReplyDelete
  29. ohhh awesome! this worked for me.. now need to go back uploading photos.. tnx!
    ~K..

    ReplyDelete
  30. Thank-you so much for explaining how this all works and sharing the code to put like buttons on a blogger page! I spent hours yesterday reading different pages trying to understand how to do this. Finally I managed to put in like buttons, they appeared on each post, but whichever like button I hit, all of them showed a hit. Your explanation helped me have a like button that acted independently for each post.

    Encouraged by your suggestion to experiment with where to put the code I decided to see if I could change the style of like button from the one in your example- and I managed to do it! I am someone who quakes at the sight of code, so to have actually changed some and make it work is a miracle! Thanks again for sharing your expertise. A:)) Now let's see if I can add like buttons to my website....

    ReplyDelete
  31. Hi there,

    Great post. I am having a problem where the like for my individual post is the same as the one for the whole blog. How do I separate them?

    http://degustationvoyage.blogspot.com

    Thanks

    ReplyDelete
  32. Thanks so much!!! That worked perfectly =)

    ReplyDelete
  33. Thanks a million! Works like a charm.

    ReplyDelete
  34. i'm using the simple template in blogger and cannot find "" and have even tried pasting the code into the HTML side of a single post and it gives me an error that the iframe is not closed.

    ReplyDelete
  35. How do I do it such that it does not ask me to comment?

    ReplyDelete
  36. After adding the like button it is asking me to confirm when I click the button. What do I do remove the confirm request?

    ReplyDelete
  37. Dear, Thanks a lot for the information..But how to place it on left side of the post?

    ReplyDelete
  38. Thanks for this... it's gotten me very close to what I want.

    I've got it so the LIKE button shows where I want it, on each blog post, and when someone hits LIKE, it results in them liking that specific post, rather than the entire blog, which is exactly what I want.

    The only problem I have is, when the resulting post appears on the persons Facebook wall, showing that they liked my blog post, attached to that notification is an image. Unfortunately, the image that gets attached is, for some reason, my Statcounter button that appears on the footer of my blog.

    Is there a way to tweak this so that, if an image is included in my blog post, a thumbnail of that image will show up with the LIKE notification?

    Or if not that, is it possible for me to specify a particular image (eg. I could make it be a thumbnail of my blog heard, instead of the Statcounter button).

    Or, worst case, is there a way I can force the notification to not include ANY image? (rather have the LIKE notification on Facebook have no image, than the Statcounter image).

    ReplyDelete
  39. Mister i owe you 1 million thanks!!!!!!

    I have broken my head again and again over the past 2 days how to figure out and get this shit of facebook to work.

    No others words to say.

    ReplyDelete
  40. THanks Devfuel, I was able to achieve what I wanted by setting

    {meta content='MY IMAGE URL' property='og:image'/}

    in my blogger template, which allows me to show a desired image for each LIKE.

    Works well.

    ReplyDelete
  41. Thank you very much!!!!!!! I've spent an entire evening with this... tried don't know how many ways from how many websites.. Until I found this website. it worked the first time. The only strange thing, that after everything was done, few seconds later, on some of my posts I had 24, 3 or 2 "likes".. and my blog is not that popular yet (most of the posts had zero likes).. Do u know if that's facebooks fault or I did something wrong.
    thank again

    ReplyDelete
  42. I can't even begin to make this work. Like so many others, I've spent days trying. I can't even find where to paste the snippet code after scrolling about three miles of html code three times. After ten minutes of scrolling gibberish, everything looks the same.

    ReplyDelete