How to Display Facebook Fans Count as Text in WordPress site

How to Display Facebook Fans Count as Text in WordPress site

You can add your Facebook fans count in different ways. The easiest way to display fans count is by using the official Facebook  Like Box social plugin. But what if you don't want to slow your site down by loading Facebook’s code? Or what if you are making something very custom and need to display Facebook page fans count as text instead of a box. Well, then you will like this tutorial. In this article, I will show you how to display your Facebook fans count as text on your WordPress site.

The first thing you need to do is to open your functions.php file, located in your WordPress theme, and then copy the following code and paste in it.

<?php
function lj_fb_count($fb_ID=''){ 
$fb_count=''; 
if($fb_ID && get_site_transient('lj_fb_count')===false) { 
$url='http://api.facebook.com/method/fql.query?format=json&query=SELECT+fan_count+FROM+page+WHERE+page_id+IN%28'.$fb_ID.'%29';
$fb_count = json_decode(wp_remote_fopen($url),false); 
$fb_count=$fb_count[0]->fan_count;

//Caching count for Ten hours----------------------------
set_site_transient('lj_fb_count',$fb_count, 60 * 60* 10 );
}else{
$fb_count=get_site_transient('lj_fb_count');
}
echo $fb_count;
}
?>

Now add this line of code in your theme template where you want to display your Facebook fans count. This could be in the sidebar.php, header.php, or basically anywhere you like. You can use your Facebook Page ID  as the function's input value.

<?php lj_fb_count('114877608587606');?>

That's it. You are done. I hope that this article helped you show Facebook fans count as text in your WordPress site/blog.  To get more such useful tips to consider following @lineshjose on Twitter. You can download this script from my GitHub repository.