Displaying the Total Number of Twitter Followers as Text on WordPress

Posted on November 8th, 2009 by in Tutorials | 15 Comments  
Displaying the Total Number of Twitter Followers as Text on WordPress

Yes it is great that you can have a twitter follower button from many sites. But what if you are trying to make a very custom design where you want to place a code in text instead of using a button. Well then this tutorial is for you. In this tutorial we will show you how you can display the total number of twitter followers as text on your WordPress site.

Create a new file called twitter.php file located in your themes folder. Remember to make sure that you change WPBeginner to your username.

<?php
$tw = get_option("twitterfollowerscount");
if ($tw['lastcheck'] < ( mktime() – 3600 ) )
{
$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=wpbeginner');
if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
$tw['count'] = $match[1];
}
$tw['lastcheck'] = mktime();
update_option("twitterfollowerscount",$tw);
}
echo $tw['count'];
?>

Then simply paste this code any where you want in your theme files.

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

Now you can display the number of twitter followers on your WordPress site as text.

Source: Rarst

About

Editorial Staff at WPBeginner mainly Syed and David.

Post comment as twitter logo facebook logo
Sort: Newest | Oldest
zoomingjapan 5 pts

I get the following error message:

http://pastebin.com/iif9g35a

Could you help me figure out what's going wrong?

Thanks a lot!

Do you know how to combine twitter and feed burner subscribers into one number?

Yes, there are two ways. One, you use this technique to echo the twitter count and then use the feedburner technique mentioned in another post on this site and echo that count. Then simply use the php add function to add those two counts and display the total. You can also use the plugin called Subscribers Text Counter

nice tutorial.. i will try in my web

Where exactly do I put this?

won't find it! i placed it on the theme folder and usng and on the actual themes folder and it can't find it

It is suppose to go in your actual themes folder.

When I try to use this, I get the following error:

Fatal error: Maximum execution time of 30 seconds exceeded

any idea what could be the issue?

I too have gotten the same error, and upon receiving the timeout error it also stops loading the remainder of my site (i.e. sidbar, follow on posts past the first one).

Also, thanks for keeping this site going it been such a big help.

Try the new code.

:) this tut has been around awhile. another good one!

Great simple tutorial,
Really useful.. thanks man ! ;)

Tweets about us: