DevelopmentHacks

Integrate Facebook’s “Like” Plugin into WordPress

21/042010 {72} comments

Facebook recently announced the take-over of the internet with the introduction of the yet infamous “like”-button. Nevertheless, here is how you can add your own dynamic like-button for WordPress.

If you use Facebook’s generator here, you’ll get some content very similar to mine:

<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fcordobo.com%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:500px; height:60px"></iframe>

Note: I highlighted the parts I replace in the next step

This would be fine, if you had only one page on your website, as the plugin only shows the url you have entered in the generator – no dynamic pages. So here’s how you can add some dynamic flavour to your new button:

Replace the following code

[…]like.php?href=http%3A%2F%2Fcordobo.com%2F

with

[…]like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>

insert it into your theme’s single.php and upload it to your server.

The final code, ready for copy & paste

This is the final code you can use in your own WordPress installation without further modifications (except the width and height). Insert it into your single.php and upload the modified file to your server.

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

Note: I highlighted the parts I replaced

In case you use some caching plugin, clear the cache and see the magic as it happens.

If you like it, click on “like” ;-)





Advertisement

72 comments

  1. NewsShit! says:

    Ich habe den Code mit dem “Better Feed” Plugin in meinem RSS-Feed untergebracht.

    Wer den RSS-Feed direkt liest, der kann dann auch dort klicken. :-)

  2. Francis says:

    Hi.

    Which part of the single.php should I add the code in?

    Thank you.

  3. Pablo says:

    Yours is a beautiful word press, sir! Cheers for the small tutorial here!

  4. Thanks very much for this tutorial. Really well designed website you have here as-well. Loving the previous and next entry links. This site has the wow factor!

    Dan

  5. Hetal says:

    Nice tip… better than using one more plugin for such a simple task. Thanks.

  6. Big Pinck says:

    I get an error message when I use the above code in my simple HTML. Can you assist with a remedy?

  7. jamie says:

    hey, my theme does not have a “single.php” file… what do I use instead?

    Thanks!

  8. Praveesh says:

    Really helping post…thank you

  9. erteljune says:

    klasse dokumentiert.
    aber bei mir funktioniert es auch nicht: button ist da, der code im quelltext sieht auch richtig aus, aber facebook nimmt die “likes” nicht an… hast du eine idee, woran das liegen kann?
    gruß jan

Trackbacks /
Pingbacks

  1. “Gefällt mir” im eigenen WordPress-Blog | Deltanews.de
  2. Agregar el botón Me Gusta de Facebook en Wordpress
  3. Facebook – Synes godt om-knap på WordPress blogs | Lisa Risager | Jeg ♥ WordPress

Leave a Reply


  1. LinkedIn
  2. Xing
  3. Facebook
  4. Twitter
Back to Top