codeigniter: facebook social plugin – like button helper

Hello everybody,

I have just created a short helper to create facebook like button dynamically for any content, by just giving-in the URL of the page. Hope this one also helps you like my other helpers :)

Helper function:

  1.  
  2. if(!function_exists(‘facebook_like’)){
  3.  function facebook_like($url){
  4.  $formlink=‘<iframe src="http://www.facebook.com/plugins/like.php?href=’;
  5.   $formlink .= urlencode($url);
  6.   $formlink .= ‘;layout=button_count&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe>’;
  7.                
  8.   return $formlink;
  9.   }
  10.  }
  11.  

Usage:

  1.  
  2. <?php
  3. $url = current_url();
  4. echo facebook_like($url);
  5. ?>
  6.  

Note:
As you can see the function requires you to use the codeigniter’s native URI helper.

Share

About thephpx

I am a freelance web application developer. I work with PHP5 and MySQL. Most of my projects are done using the codeIgniter framework.I am also working towards moving onto the Zend Framework. I follow agile development methodology and manage my projects throuh SCRUM based approach. Apart from being a developer I am also the CEO of a small web identity and application development firm located in dhaka, bangladesh. The firm known as 'implode impressions' also follows agile development techniques and works under the SCRUM based approach.

7 responses to “codeigniter: facebook social plugin – like button helper

  1. pusp raj joshi

    Nice one work for me …
    expect other things which i will search..
    Thanks thephpx….

  2. edit this to:

    function facebook_like($url = false){
    if(!$url) $url = current_url();

    Then the actual function call can simply be

    or

    If you want to send in a different url, simply pass it through as the parameter, if not the current url will be used. I have the identical helper on AngelCMS.co.uk and all my sites! (Was looking for something else, found this but thought I could help anyways)

  3. Thanks bro , i like it and was very much useful to me .. thanks

  4. Tal

    I must be doing something wrong because I see the raw PHP code surrounding the like button on my page. These are the steps I took:

    (testing on localhost)
    1. copy code as is – paste in a file called facebook_helper.php and save to directory application/helpers/

    2. in my controller I’m loading with the call $this->load->helper(‘facebook’);

    3. in my view I’m using the code described in the usage box above.

    Any help or guidance would be much appreciated. Thanks.

  5. hi there,

    the steps looks file, but have you loaded url helper before calling that because if you see it usage the current_url() function which is dependent on the url helper.

    thanks,

    faisal ahmed

  6. Tal

    hi Faisal,

    Thank you for the reply. I should have mentioned I have the URL helper autoloaded in autoload.php using the following line:

    $autoload['helper'] = array(‘url’);

    Is there anything else I might be missing? Thanks.

  7. Tal

    I’m really embarrassed to admit that I had a simple syntax error and it is working fine now…sorry to take up room on the board….Great helper function, thanks for your work and for your help!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>