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>


Share

Tags: , , , , , , ,

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