How To Display Twitter Followers As Text
February 9th, 2012 - 11 Comments »
In a previous post I showed you to display your Feedburner Subscriber count as text, now here is the code to display your twitter followers as text.
Step 1
Create a new file in your chosen code editor and copy and paste the following code into the file:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $tw = get_option("twitterfollowerscount"); if ($tw['lastcheck'] < ( mktime() - 3600 ) ) { $xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=handyHTML'); if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) { $tw['count'] = $match[1]; } $tw['lastcheck'] = mktime(); update_option("twitterfollowerscount",$tw); } echo $tw['count']; ?> |
Where on line 5 it says “handyHTML” change that to your Twitter User. Once that is done save the file as twitter-count.php, save the file into the root of your websites directory.
Step 2
Now copy and paste the following code where you want the count to display, this calls the twitter-count.php file we created in Step 1 and outputs the total followers of the specified Twitter account.
|
1 |
<?php include("twitter-count.php"); ?> |
Step 3
Finally you can style your Twitter count, here is an example:
HTML
|
1 |
<p><span class="twitter-count"><?php include("twitter-count.php"); ?></span> Followers</p> |
CSS
|
1 2 3 4 5 6 7 8 9 |
p { color:#fff; } span.twitter-count { font-size: 22px; padding-left: 5px; color: #1e7c9a; } |
…and voila! Come across any problems, comment and one of us will assist you.
Related posts:









This is the exact thing I am searching for last few hours.. the next thing I need to figure out is a way to display FB Likes as text.
Hi Raj!
Try this http://www.handy-html.com/another-simpler-way-to-display-facebook-fan-count-as-text/ this shows the number of “likes” or fans a Facebook page has.
Thanks.
George
Hi,
Great tutorial!
It’s all set up correctly. How long time will it take before Twitter responds? Because nothing is showing now.
Thanks!
// Tom
Hi Tom,
Apologies, the PHP include path was wrong, I have just updated the code above.
Let me know if you have anymore problems.
George
Thanks! It works fine now.
I want to add several counts on to my page. I’m about to make a table of fifteen (15) different account. How can I do that?
Hi Tom,
Off of the top of my head one way would be to make 15 files or however many accounts you want to use and just name them seperate to each account. (twitter-1.php, twitter-2.php, twitter-3.php and so on). Each file having its own account details in.
Then simply include each file where you want each count to display.
Note: this may not be best practice way to do this, although it will definately work. No matter what way you do it you’ll have to make 15 calls to twitter to get each accounts count anyways…
Hope this helps.
George
Hi,
I am getting an error which says
Fatal error: Call to undefined function get_option() in /home/wwwlinu/public_html/theplayers/inversore/twitter-count.php on line 2
Please let me know what is missing in my code.
Yes i am also getting the same error, some buddy please respond!
I will have a look into this for you. Send me a preview where you are running this code please?
George
this works but i am searching for a method, so that twitter follower is shown within the twitter button….
Twitter have change the URL for the api, change the following:
$xml=file_get_contents(‘http://twitter.com/users/show.xml?screen_name=handyHTML’);
to:
$xml=file_get_contents(‘http://api.twitter.com/users/handyHTML’);
Al