The code snippets below will provide you a function that you can echo anywhere on your site, to get the count for each social service. The snippets below will let you get your Twitter follower count, your Facebook like count, and your Google+ circle count.
Let’s take a look at the code.
Twitter Follower Count
function twitter_count(){
$count = get_transient('twitter_count');
if ($count !== false) return $count;
$count = 0;
$dataOrig = file_get_contents('http://twitter.com/users/show/wpforce');
if (is_wp_error($dataOrig)) {
return 'Error!!!';
}else{
$profile = new SimpleXMLElement ( $dataOrig );
$countOrig = $profile->followers_count;
$count = strval ( $countOrig );
}
set_transient('twitter_count', $count, 60*60*24); // 24 hour cache
return $count;
}
To use this function, simple echo it wherever you want it.
<?php echo twitter_count(); ?>
Facebook Like Count
function fb_count(){
$fb_id = '291541550876794';
$count = get_transient('fb5_count');
if ($count !== false) return $count;
$count = 0;
$data = wp_remote_get('http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id='.$fb_id.'');
if (is_wp_error($data)) {
return 'Error!!';
}else{
$countOrig = strip_tags($data[body]);
$count = preg_replace('/\s+/','',$countOrig); // strip whitespace
}
set_transient('fb5_count', $count, 60*60*24); // 24 hour cache
return $count;
}
To use this function, simple echo it wherever you want it.
<?php echo fb_count(); ?>
Google+ Circle Count
function gplus_count(){
$count = get_transient('gplus_count');
if ($count !== false) return $count;
$count = 0;
$data = file_get_contents('http://widgetsplus.com/google_plus_widget.php?pid=YOUR_GOOGLE_PLUS_ID&host=PUT_YOUR_WEBSITE_HERE.com');
if (is_wp_error($data)) {
return 'whoa error!!!';
}else{
$match = preg_match('/<strong>(.*?)<\/strong>/s', $data, $results);
if ( isset ( $results ) && !empty ( $results ) )
{
$count = $results[1];
}
}
set_transient('gplus_count', $count, 60*60*48); // 72 hour cache
return $count;
}
To use this function, simple echo it wherever you want it.
<?php echo gplus_count(); ?>
REMINDER The functions above use the set_transient() function which is built into WordPress, but you could swap out that function to use local disk cached files if you need to use this outside of WordPress.
UPDATE – 04/12/2012: Google+ changed it’s design and the code, so there was a small tweak to the G+ code. It now uses widgetsplus.com as the source, because the Google+ code became unreliable. Hat tip to Pedro for suggesting the new regex.
Each function is hard coded to use my username data, so you’ll want to adjust it to use your own data.
If you have any feedback or questions about the functions, please leave a comment below.


Jonathan Dingman is a passionate blogger who loves writing about WordPress news, reporting on events, theme releases, awesome plugins, and more. He started using WordPress in 2004 and ran the first WordCamp NYC in 2008.
Hi!
That’s exact what I’m looking for. Works great in WordPress.
Please help me to get this outside WordPress.
I read your “Reminder”, but I don’t know how to “swap out that function to use local disk cached files”. Could you please post a code, that works php only?
Thanks
Ralf
Hi Ralf,
The focus of this site is more about WordPress, rather than building functions that work for any platform. Sorry, I won’t be developing the code to work outside of WordPress.
Try doing a google search for “caching files php” and see what you can come up with, there are plenty of great resources out there that will teach you how to do it.
Just swap out the set_transient() function for the caching call once you have the file caching layer in place.
Get circles count in that way don´t work anymore…. Google plus has changed the accounts layout and the circles count is now loaded by Javascript in new account layout. Plus API doesn’t share user circle count. The only way I think is catch the Circle Count that is in the “plus widget” (http://widgetsplus.com/). What do you think about it ?
I’ll look at it and see what needs to be done, I can probably whip something up. Thanks for the heads up.
Don’t worry, your code worked since 1 or 2 weeks ago. But now it doesn’t, either for me (my own code). I archive a solution that works with the Google plus widget from Google (http://widgetsplus.com/):
$datos_plus = file_get_contents(‘http://widgetsplus.com/google_plus_widget.php?pid=’.$googlePlusID);
preg_match(‘/<strong>(.*?)<\/strong>/s’, $datos_plus, $coincidencias);
$circulos = $coincidencias[1];
echo $circulos;
This will works for a long time (until Google change the widget layout :P), you can change a litte the code for cache etc…
Hey Pedro, nice one but how are you displaying just the count? When I use the widgetsplus.com script in my code it’s displaying the whole box and not just the count. Be great if you could help.
That’s what the preg_match is for.
I updated the code on the page to reflect what we’re using here on wpforce.com, give it a try.
Thanks Jonathan, will give it a go now. :)
Pretty new to all this, I’ve been looking at a way to convert the counts into a number format, but don’t seem to be having much look any tips on how to use the number_format method for larger counts.
ta
Hi!
The http://widgetsplus.com seems to be offline … any new solution to get the number of circles ?
best greetings
Looks like http://www.circlecount.com/ might fit the bill. I’ll try to do some research soon and see if I can find a new solution that works within PHP.
Really good stuff man. Thanks for sharing ;)
I want to dispaly alexa rank of my own website in the page (Only one page not throughout) SO please help me how can i insert the code, I mean where I should insert the code and how to call in in wordpress ?
After a quick search, I found this page that should help you with that.
Thank you, I knwo the code but how to do it in wordpress, leave about alexa suppose I want to display the FB likes of my own URL on a post on my blog, how can I do it? I mean where to put that php codes?
I am waiting to hear from you.
You would need to modify the code and put it in your functions.php file within your theme.
Hi, great! I got it working except for Google. Is there any way to get this work?
Kind regards Willem
Just wondering if you missed my reply ;-).
Hi Willem,
I’m not sure what you mean by your comment. What about Google? What issues are you having?
Hi Jonathan,
It just doens’t work while Facebook and Twitter do…
Do you have the same experience?
Kind regards,
Willem