Facebook Like Buttons with Dynamic URLs in PHP and JavaScript

Facebook Like buttons are popping up all over the Web as a way for people to integrate a little bit of Facebook into their site. I thought it’d be a fun experiment to try on my site here, but I wanted a dynamic URL to pass. There are a ton of blogs out there for putting a Facebook “Like” button your pages using a dynamic URL with a bit of PHP trickery. Here’s the code I ended up using for this site:

<iframe src="https://www.facebook.com/plugins/like.php?href=<?php echo 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];?>&layout=button_count&show_faces=false&width=80&action=like&font=verdana&colorscheme=light" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:80px; height:22px" allowTransparency="true"></iframe>

However, on another site I’m working on getting this implemented on, I don’t have access to the raw code, and can’t use server-side scripting or variables. So I came up with a JavaScript solution that I’m sharing here as I couldn’t find an equivalent version online:

<SCRIPT LANGUAGE="JavaScript">

document.write("<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href="+document.URL+"&layout=button_count&show_faces=false&width=80&action=like&font=verdana&colorscheme=light\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:80px; height:22px\" allowTransparency=\"true\"><\/iframe>");

</SCRIPT>

It’s probably not the cleanest/best code in the world (I would never consider myself a programmer) and there are probably 100 different ways of doing this, but this worked for me and hopefully that will help somebody out there on the InterWebs (and feel free to “Like” this post — the button’s on the permalinked URL — just to see if it works). You also might want to adjust the width of the frame and the code a bit. I personally just wanted a small button with just the number of likes (and am considering even getting rid of that, tightening it up a bit).

And for my future reference, these two tools were very useful in doing this — escaping the code for posting here, and creating that document.write statement.

Note: I’m now using the add-this.com service for this, as it provides me better tracking and features than my code above does, but I’m leaving this here for reference.

Comments

Nathan says:

First! 🙂

Patrick says:

Very cool code. I was wondering if there was a way to make the like when clicked , give the user the option to write a comment to their profile.

Jake says:

You’re on your own there, Patrick, as I haven’t played with this code much since adding it here (other than a few site-specific tweaks).

idan says:

Great
thx
🙂

bin says:

By the way nice Code
But there is a problem
because in my profile i see i clicked the “Like ” but from other facebook account he cant watch that i Liked that Links, means harder to get link in

Vlada says:

Why change my ? signs into %3F ?

Jake says:

%3F is the escaped version of ?, and should probably work fine as well, but don’t know for 100%. This code is provided as-is, and probably could be done a bit better.

Reading Material

I wouldn’t expect much in the way of posting over the weekend, as I plan on unplugging and spending time with family as much as possible. That, and I’ll be…

eeee says:

eeeeeeeee 😀

xia says:

Hey thanks for this
You mention SSI; the Iframe version requires an escaped URL, do you know how to do this with SSI?
tia

Anonymous says:

poo

bob says:

this does work the same as another javascript code I have found but the trouble with the dynamic pages is when it is liked like on a blog with an image it only shows the image instead of the text from the blog also. I have taken a similar page and had it on my main page and it displayed the image and the text from one of my blogs, that is what im looking to achieve I want it to show the image and the text from the blog I don’t know why iot just grabs the iamge from the page and not the text

Manu says:

Thanks a million dude! You solved my problem! I spent today 5 hours in order to find a way to do this!
MN

Anonymous says:

I need to show the image and also site url.
I dont know how to give it. can u pls help me.

Anonymous says:

rdsdss