How to Get Twitter Follower Count in WordPress?

Pin It


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:

3 total comments on this postSubmit yours
  1. I think you must modify the appearance of your code, it’s show as < :(

  2. I don’t like embedding foreign javascript on my site, it makes it insecure. I’d rather have straight html, like this widget.

  3. Take a look at the Transients API:
    http://codex.wordpress.org/Transients_API

    Other than that, thanks for sharing.

12 total pingbacks on this post
Submit your comment

Please enter your name

Your name is required

Please enter a valid email address

An email address is required

Please enter your message

Twitter

  • May 2, 2012 04:15

    10 Free WordPress Theme To Make You Forget About Premium Theme http://t.co/YRiLCiVa

  • March 2, 2012 17:16

    3 Helpful Tools to Test Cross Browser Compatibility of Webpage http://t.co/FY0jh8Dn

  • March 2, 2012 16:43

    3 Helpful Tools to Test Cross Browser Compatibility of Webpage http://t.co/QUlYKInf #IE #FF #Chrome #Testing #CrossBrowser

  • March 2, 2012 12:52

    Top 5 Plugins to Build Contact Form for WordPress http://t.co/PWyR08Iq #WordPress #Plugins #ContactForm

  • June 11, 2011 02:49

    CSS 3 Transitions http://bit.ly/m0pK3t

Design Gala © 2012 All Rights Reserved

Designed by 76miles

Powered by WordPress

More in How to, WordPress (27 of 60 articles)