Twitter Follower Count in Plain Text

It’s easy to use a service like TwitterCounter.com to get a little chicklet button of your Twitter follower number. If you have that number in plain text though, then you can style and integrate it into your site much more attractively. In this post, I’ll show you how to get it.

You can see the end result in the top right of Tiny Buddha, a site I recently coded using Chameleon (Though this will work on any WordPress theme!)

And you can download the completed code here.

Quick Overview

To do this, we will create 3 functions, to:

  1. Get the follower count from Twitter, and save it to your database.
  2. Retrieve that count whenever you need it.
  3. Update the count automatically every hour.

We add in the extra steps of saving to your database and updating because on a busy site, Twitter will cut you off for pestering their servers too much.

1 – Get The Follower Count from Twitter

There is an easy way to get an XML file from Twitter, which has all of the details about your profile. We’re going to use that file to find out your follower count.

You can see it at http://api.twitter.com/1/users/show.xml?screen_name=YOURNAMEHERE (e.g. Here’s mine)

Start out by opening up your theme’s functions.php file (Or create a file with that name if your theme doesn’t have one. Chameleon users would do this in chameleon>custom>functions.php).

The first thing we are going to do is create a function which gets the follower count from Twitter, and saves that value to your database. Copy and paste the following between the tags of your functions.php file.

function update_twitter_count() {
 
$name = 'problogdesign';
$url = 'http://api.twitter.com/1/users/show.xml?screen_name='. $name;
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
 
$xml = new SimpleXMLElement($data);
$count = $xml->followers_count;
 
$count = (float) $count;
$count = number_format($count);
 
add_option('twitter_followers');
update_option('twitter_followers', $count);
}

At the very top, replace the name problogdesign with your own Twitter account name.

Now, let’s take a look at what the code inside this function is doing.

  • The first 2 lines (Starting with $name =…) simply save the address of the XML file on Twitter.
  • The next 5 lines (Starting with $ch =…) use cURL to get that file and allow PHP to work with it. cURL is a large topic that could do with several posts of its own, but if you want to find out more about it, take a look here.
  • The next 2 lines (Starting with $xml=…) allow us to get one specific value from the XML file, and save it to the $count variable.
  • The 2 lines after that (Starting with $count =…) cast the string as an integer (number), and then format that number with commas (e.g. 7686 becomes 7,686). You can also set how number_format works (e.g. some European countries would list that as 7.686)
  • The add_option line creates a new row in the wp_options table of your database, for storing a value called ‘twitter_followers’. When you next run this and the row already exists, this line won’t do anything.
  • The final line, update_option, updates the value of ‘twitter_followers’ with the count we have just worked out.

2 – Retrieve the Value

This next function is just one line to print the value from the database option we just saved.

Copy and paste this below the previous function:

function twitter_count() {
	echo get_option('tb_twitter_followers');
}

3 – Update Hourly

Last of all, we are going to use the WordPress Cron to automatically run our update function every hour. Copy and paste this final function below the other two:

if (!wp_next_scheduled('your_hourly_hook')) {
	wp_schedule_event(time(), 'hourly', 'your_hourly_hook');
}
add_action('your_hourly_hook', 'update_twitter_count');

With the first 3 lines, we create a hook that is processed once every hour.

In the final line, we use add_action to attach our update function to this hourly hook. And you can attach as many other functions as you want to it.

If you’d like to learn more about scheduling events with WordPress, take a look at WP Engineer’s tutorial.

How to Display the Count

Now, anywhere in your template you can write and the number will be printed to the page for you, e.g.

<a href="http://twitter.com/problogdesign"><?php twitter_count(); ?> Followers</a>

NB – This won’t work until the other functions have run once. You may want to manually call update_twitter_count() once to force it to happen right away (And then delete that call again!), or just wait an hour for the first scheduled run of it to happen automatically.

And that’s all there is to it! You can download a complete sample functions.php file to use in your theme here (just rename it to functions.php).

If you have any questions, feel free to ask them in the comments!

Enjoy this post? You should follow me on Twitter!