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

Tagged: , , , , , ,

Comments: 7

  1. pusp raj joshi November 12, 2010 at 13:43

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

  2. James April 20, 2011 at 03:18

    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. Anand Godar April 27, 2011 at 11:41

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

  4. Tal July 19, 2011 at 07:46

    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. thephpx July 19, 2011 at 08:37

    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 July 21, 2011 at 06:53

    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 July 21, 2011 at 07:13

    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 *

*