How to get Twitter Follower Count in WordPress?

February 7, 2010 |  by Anish Blon
ebeceff492d8b49adac3555c40940c07 Del.icio.us

I am neither the first nor the last one that will be writing about placing twitter follower counter in webpage. Though there already exist many widgets anyone can easily use and customize; see here and here; But those widgets have limited options in terms of color, layout and moreover they have their info attched (somewhere) in widget which does not feels good. Here’s my attempt on how to get follower count in twitter using PHP.

Step 1:
We will create a PHP file; lets say follower_count.php or whatever you’d like to name it. Then simply put following contents in that page.

<?php
$twitter_username	=	'designgala';	// please replace it with your twitter username
$tw	=	get_option("total_twitter_followers");
if ($tw['lastcheck'] < ( mktime()3600 ) ) { 	$xml	=	file_get_contents('http://twitter.com/users/show.xml?screen_name='. $twitter_username); 	if (preg_match('/followers_count>(.*)

Explanation:
I used 2 PHP functions file_get_contents() and preg_match() here. file_get_contents fetches twitter xml file and stores in buffere while preg_match searches followers_count for a match in the buffer returned by earlier function.

Step 2:
Now you can include above file using PHP include like below in any template file.

<?php include("follower_count.php"); ?>

Thats it. It will show twitter follower count.

[Photo Credit: JavaJunky via Flickr]



No Comments


Trackbacks

  1. Design Gala
  2. Design Gala
  3. Superior Network
  4. WordPress Rocker
  5. Anish Lama
  6. Pierpaolo Frigerio
  7. topsy_top20k
  8. uberVU - social comments
  9. Saroj Pandey
  10. Gayath chaminda
  11. Barbara Wilson

Leave a Reply