Sunday, July 25, 2010

WordPress: Add Facebook Like Button using functions.php

The Facebook Like button lets a user share contents with friends on Facebook. When the user clicks the Facebook Like button, a story appears in the user's friends' News Feed, with a link back to the original website.

[caption id="" align="alignnone" caption="Facebook Like Button"][/caption]

Append this code snippet to functions.php file in theme or child theme folder to add the Facebook Like Button.
function add_fb($content) {
$fb = ''; // display only on single page.
if (is_single()) {
$fb = '<iframe src="http://www.facebook.com/plugins/like.php?href=';
$fb .= urlencode(get_permalink(get_the_ID()));
$fb .= '&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;';
$fb .= 'action=like&amp;colorscheme=light"';
$fb .= ' style="border:none; overflow:hidden; width:450px; ';
$fb .= 'height:60px"></iframe>';
}

// display before the content.
return $fb . $content;

// display after the content.
// return $content . $fb;
}

add_filter('the_content', 'add_fb');

Please visit http://developers.facebook.com/docs/reference/plugins/like for more information on how to customize the Like Button.

5 comments :

  1. This is great! Now I won't be needing a plugin for this. Thanks for the snipppet!

    ReplyDelete
  2. So how do I change the language of the like button in this function?

    Thanks in advance!

    ReplyDelete
  3. @Celina: You can append locale=nb_NO to variable $fb to change the language (locale) of the Like button to Norwegian (bokmal).

    ReplyDelete
  4. It works wonders, thank you so much!

    ReplyDelete
  5. I also want the like button on Single Page. What can I do?

    ReplyDelete