facebook like button code with dynamic page URLs on your website is EASY!

So, you’ve decided to “upgrade” your site to include some like buttons for facebook? I recently had to do this for a customers website and was surprised at how simple it really is. The only difficulty is if you have a dynamic page, IE a product or blog style page that the code may need to reflect the changing URL. See below for more info.

For an excellent Facebook like page/button creation tool click here, facebook provide one!

  • The generic (non dynamic URLs) Code is something like this… as seen on this page here

<iframe src="http://www.facebook.com/widgets/like.php?href=http://example.com"
        scrolling="no" frameborder="0"
        style="border:none; width:450px; height:80px"></iframe>

  • Here I’ll show you how I updated my code for a CRE Loaded website to insert this snippet below the product description of the product_info.tpl.php file effectively adding this option to every product on the site with the dynamically updating URL!
    Note the part in BOLD it is the bit I changed.

<iframe src="http://www.facebook.com/widgets/like.php?href=<?php  echo "http://" . $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; ?>"
        scrolling="no" frameborder="0"
        style="border:none; width:450px; height:80px"></iframe>

So <?php  echo “http://” . $_SERVER[‘SERVER_NAME’].$_SERVER[‘REQUEST_URI’]; ?> grabs the current URL of the page and inserts it into the iframe so facebook knows where the “like” hit comes from and where to send prospective customers from the social networking site.

You can use the generic code at the top of this post if required for a standard non changing static page, or the bottom one for a dynamic page :) .

NOTE: This is for a php based site, obviously calling <?php requires that the page be a .php page and also that the server supports it.

UPDATE: If your keen for a link like the “Recommend” one I have on the right of this page in a widget, here is the code.

<iframe src="http://www.facebook.com/widgets/like.php?href=<?php  echo "http://" . $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; ?>&layout=button_count&show_faces=false&width=110&action=recommend&font=tahoma&colorscheme=light&height=30"
        scrolling="no" frameborder="0" style="border:none; width:110px; height:30px" allowTransparency="true"></iframe>


Tags: , , , , , , ,

12 Responses to “facebook like button code with dynamic page URLs on your website is EASY!”

  1. andrew February 22, 2011 at 2:59 am #

    how would you dynamically update a plain HTML site using either your code or the XFBML? Thanks!

    • Josh March 7, 2011 at 12:24 am #

      Sorry Andrew, I am unsure on this one. Perhaps someone knowing xfbml could help here?

  2. Georges July 5, 2011 at 10:35 am #


    My pages are linked this way: http://www.example.com/Folder/?p=test
    When i copy your code i can see the like button and i click it.
    When I click it, it counts my like but doesnt link it to my profile on facebook.
    Now if I change to “http://” to something else… let’S say “test”.. It links the link to my facebook like this: http://testwww.example.com/Folder/?p=test
    Can you help me there?

    • Josh September 25, 2011 at 10:02 am #

      I’m sorry mate I don’t understand your issue here? does it count the link and increment the number? Ofcourse only 1 like possible from each person.

  3. Vikash Rajput July 20, 2011 at 5:32 pm #

    Sure. will work for sure.!

  4. Karen Mussoline July 26, 2011 at 8:13 am #

    Works like a charm, thanks!

  5. Rob August 13, 2011 at 9:29 pm #

    Hi There.

    am trying to implement this on my site, but I cant seem to get it working..I post the code into that file but nothing…. Has anything changed since you posted this article?

    Any help would be much appreciated!

    • Josh September 25, 2011 at 9:59 am #

      Nothing should have changed, I have that code working on a few sites without touching it. post your code here and I’ll take a look. Cheers

  6. Jim February 8, 2012 at 11:30 am #

    I am trying to add a dynamic id like “http://www.dummy.com/dummy.aspx?id=1000”, but Facebook Like button only recognized http://www.dummy.com when I checked facebook. Please help.



  7. Blured December 6, 2013 at 3:49 am #

    Nope, doesn’t work

    • Josh January 22, 2014 at 1:53 am #

      Sorry mate, this is very old code. Facebook have probably updated their systems since I posted this. They usually have pretty good documentation. I dont really have time at the moment to review this and re-write it sorry.

  8. Akash.k.v July 25, 2014 at 5:13 pm #

    YOu save my day,, thanks bro

Leave a Reply