Facebook, as you may know, is attempting to own a greater share of the web by allowing you to put a facebook “like” button and other social widgets on any web page. Think what you like about Facebook, their questionable privacy, and their evil plans, but the fact remains that it is the #4 most visited website today and growing. I’ve just sort of accepted that if someone really wants to they can know pretty much everything I’ve ever done online. Meanwhile, getting Likes in lots of people’s profiles could be some some potent leverage for me.
I’m not going to reiterate the details. I’ll just link to them and/or quote them if necessary. It boils down to this: There are two ways to do a “Like” button, either insert an iframe in your site (with all the attendant ugliness of iframes), or actually enable your page and use the facebook SDK yourself.
For this blog, for starters, I chose the iframe way because it’s dirt simple. You just go to the Facebook Like Button Page. They have a handy dandy code generation form. Enter any url in it because we’re going to tweak it. Here’s the code it generates:
<iframe src="http://www.facebook.com/plugins/like.php?href=http%253A%252F%252Fwww.whatever.org&layout=standard&show_faces=true&width=450&action=like&font=lucida%2Bgrande&colorscheme=light&height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>
The problem with this easy code is that it only likes the whole site. And I would like to know what people like on an article by article basis. More importantly, if someone reads Johny’s profile I would like it to say and link to “Johny likes Hot Dorkage: New Facebook Like Button Dorkage” rather than just “Johny likes Hot Dorkage” (though I certainly wouldn’t turn up my nose at that last.) The key to that is the URL. So what I did was replace that with a little dynamic code to make the URL be the specific URL of the current blog post. In my wordpress template “single.php” I inserted the following modified code:
<iframe src="http://www.facebook.com/plugins/like.php?href=<?= urlencode('http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']) ?>&layout=standard&show_faces=false&width=450&action=like&colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:30px"></iframe>
Notice that I replaced the href with some code to calculate the URL on the fly by just appending the current request to the server name. It works just fine. Go ahead and like this post if you wish, to test it.
If you’d rather do a wordpress plugin, here’s one that will do this all for you. As far as I know, the plugins all wrap the simple iframe method of doing the like button.