X

How to Get Total Facebook Likes in WordPress

Snippets by IsItWP

Are you looking for a way to get total number of Facebook likes your posts have? While there’s probably a plugin for this, we have created a quick code snippet that you can use to get total Facebook likes and display them in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

function fbCount($url) {
      $content = file_get_contents("http://api.ak.facebook.com/restserver.php?v=1.0&method=fql.query&query=select%20url,%20total_count%20from%20link_stat%20where%20url%20in%20('".$url."')&format=xml");
      $fbCount = simplexml_load_string($content);
       echo $fbCount->link_stat->total_count;

     if(is_bool($fbCount)){
            print '0';
        }else{
            echo $fbCount;
        }
}

Add this code to your theme’s single.php file to display the total number of Facebook likes your posts have.

<?php 
              fbCount(get_permalink($post->post_id)); 
?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 27 best WordPress business themes for your website and how to create a WordPress donation form.

Comments   Leave a Reply

  1. Do I need to add anything else for this to work? It just outputs 0;

    1. @c74a43f8a3edc5e1429dda40cf329a8c:disqus nope that is it, however you may want to contact your hosting provider to make sure they have “file_get_contents” some shared hosting solutions do not have this enabled.

      1. Hector Castilla April 18, 2014 at 1:22 am

        Hi kevin!

        how to put ” like” or the hand icon after the like number?

        thanks

      2. Hector Castilla April 18, 2014 at 1:22 am

        Hi kevin!

        how to put ” like” or the hand icon after the like number?

        thanks

      3. Hector Castilla April 18, 2014 at 1:22 am

        Hi kevin!

        how to put ” like” or the hand icon after the like number?

        thanks

      4. Hector Castilla April 18, 2014 at 1:22 am

        Hi kevin!

        how to put ” like” or the hand icon after the like number?

        thanks

      5. Hector Castilla April 18, 2014 at 1:22 am

        Hi kevin!

        how to put ” like” or the hand icon after the like number?

        thanks

      6. Hector Castilla April 18, 2014 at 1:22 am

        Hi kevin!

        how to put ” like” or the hand icon after the like number?

        thanks

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!