How to Use a Facebook Like Button on Any Page

The facebook like button is in. I know, you’ve been seeing it more and more around the web and now you want to implement it on your own website. I’ll show you how to do just this using a little snippet of code that will allow you to add a dynamic Facebook “like” button to any page on your website!

Implement a dynamic Facebook Like button

First off, why would you want this? Let’s say you have a blog and in the footer (or header… or sidebar) you’d like to have the facebook like button. Well, it really isn’t very practical to have to insert this in the footer of your post everytime you create a new entry. Solution: let’s leverage the power of PHP to make the Facebook like button dynamic!

Facebook Like Button PHP Code

[inline]
[code lang=”php”]
<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo rawurlencode(‘http://’ . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’]); ?>&amp;layout=standard&amp;show_faces=false&amp;width=275&amp;action=like&amp;colorscheme=dark&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:275px; height:35px;" allowTransparency="true"></iframe>
[/code]
[/inline]

Code Explanation

What the code above will do is allow you to insert a dynamic like button in a common place on your website and have that button’s url change according to the page the visitor is on (hence why I say dynamic). For instance, on a WordPress site you could put the code in your sidebar.php file and the url to be ‘liked’ would be whatever page the visitor is on.

Extra Tips

If you don’t want the url to change and only want the user to ‘like’ one url on your site (let’s say the homepage for instance) then you could change the address to that static text. This is in fact how the Facebook like button creation tool works on their website.

Also: Don’t forget the power of PHP and, if you’re on the WordPress CMS then you can also leverage the built in functions like bloginfo(‘url’); and more! Simply replace

Hope you enjoy this snippet. Also, FYI… Facebook has changed ‘become a fan’ to just like… Questions? Leave a comment below or Contact DLOCC