How to put the Facebook ‘Like’ button on your WordPress blog

Facebook has announced its Facebook for Websites service the their f8 developers conference.  Along with a bunch of other services is a fairly simple little button you can add to any site called the Like Button.  It works in exactly the same way as liking something on your Facebook newsfeed, but it’s for any page on the internet. 

This is what the button looks like when it’s put onto a page:

Like button 

It shows the profile picture of the person who’s viewing the page (if they’re logged into Facebook) and how many other people have liked that page. You can see an example of this on my wife’s site Pea Pilly Bean *.

If you click the Like lutton on any page, it will add a like to that page & post a message on your newsfeed that you liked that page, it looks like this:

Likes on my newsfeed

On the Like button page on the Facebook developers site there’s a handy too that will allow you to generate the code for the button to put on your site. You can put any url in there & it will spit out code that looks like this:

<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.slaven.net.au%2F&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>

You can stick this on your site & it will work.  The only problem is that it’s a fixed url.  So if you use your site’s homepage url when people click it, no matter what page or post they’re actually looking at, it will always post the link to your homepage.  This is fine, but it would be way more useful if it posted that they were liking the actual post they’re on.

Fortunately this is really easy to do.  You just need to change the code to this:

<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode('http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']) ?>&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>

This will then generate a link button for the page you’re looking at, rather than for a fixed URL.  To install this either paste this code into your WordPress template somewhere, or to make it even easier install the PHP Code Widget plugin which allows you to add a widget to your sidebar that you can put PHP code into.  Then just drag the ‘PHP Code’ widget onto your sidebar in the Widgets admin section & paste the above code into it

PHP Code Widget

Click ‘Save’ and you’re done.  The Like button will now show on every page, with the button referencing this page the person is looking at.

The only thing you may have to change is the ’450′ value for the width in the code.  If you want it to be narrower or wider you’ll need to change this value

I’m sure a more sophisticated solution to this will come out fairly soon, but for now this works just fine. Update: There’s now a bunch of plugins that will replicate this functionality, check the comments of this post for some examples

Overcomplicating the issue

One of the main issues that some people have been having with the FriendFeed Comments Plugin is that sometimes, seemingly randomly, it will fail to match up a post with the information from FriendFeed.  There are a couple of reasons why this does happen, but the problems all centre around the fact that I have to use the post title to match up the details because there’s no other uniquely identifiable piece of information available.

This is of course a problem when people edit the title, either due to a typo or a editorial change.  Also there seems to be some string encoding issues, especially around the 32,000 different characters that can render to look like an apostrophe.    I’ve spent quite a lot of time trying to strip out the special characters and normalise the titles so I can be sure of getting a match between what’s on the blog & what’s coming back from the FriendFeed API.  I even tried just stripping out non-alphanumeric characters, but then I had a complaint from an amazingly patient person who’s site was using Cyrillic.  So what I’ve got now is a rather hodgepodge solution where it tries to strip out as many characters as possible, then does an md5 hash on that to use as the id, and some people are still having issues where, for no apparent reason, the posts won’t match up.

Continue reading

New release for the FriendFeed Comments WordPress plugin – remembering your details

I’ve just checked in a new version of the FriendFeed Comments plugin, which you can download here.  If you’ve already got it installed it should be showing up that an update’s available any moment now.

There’s a lot of code tidy ups and debug improvements, but the major change is the addition of the ability to remember your FriendFeed username & API Key when you comment on or like a post.  There’s now a ‘remember’ checkbox under the API Key field (which is now password masked, like it always should have been!).  If you tick that, it will drop a cookie onto your computer which means you don’t have to keep typing in your username & API key for that blog.

An important security note: the cookie doesn’t contain any identifiable information in it.  It just has a hash key value that is used to look up your username & api key in the blog’s database (which is stored in plain text in the blog’s database).  There is no way through the plugin for the site owner to see your API Key, but if they open their database directly, it is possible.  As in any other case, only share your API key with sites that you trust.  If you don’t tick the ‘remember’ box, your username & api key will not be stored anywhere.

As always, try it out here and then give it a go on your site if you like.  Any problems, please let me know.