How to show the number of Twitter followers you have in WordPress
You may notice that, over on the right hand side, next to my "Follow me on Twitter" button, there's some text that says (currently) "2,426 others do". If you want something similar on your WordPress blog, here's how to do it.

Display the number of Twitter followers you have.
First, you need to copy this code, which I picked up here, and save it into your functions.php file (via the theme editor link in the left hand column of WordPress's admin page). You'll need to change screen_name=XXX to replace XXX with your username.
function get_follower_count() {
// first check the transient
$count = get_transient('follower_count');
if ($count !== false) return $count;
// no count, so go get it
$count = 0;
$data = wp_remote_get
('http://api.twitter.com/1/users/show.json?screen_name=XXX');
if (!is_wp_error($data)) {
$value = json_decode($data['body'],true);
$count = $value['followers_count'];
}
// set the cached value
set_transient('follower_count', $count, 60*60); // 1 hour cache
return $count;
}
Next, you need to call the number that this function works out by adding some code at the appropriate place in your theme - the sidebar.php file in my case. One suggestion is like this:
<?php echo (get_follower_count()); ?> others do.
The problem with that is that if you've got more than 999 followers, there's no formatting - so 1,000 comes out as 1000 with no comma. I know. Can you imagine?
The solution is to use the number_format command:
<?php echo number_format (get_follower_count()); ?> others do.
Now if you have 1,000 followers this will display 1,000 and not 1000. Sorted (though see comment below). (Picture credit.)
[...] more here: How to show the number of Twitter followers you have in WordPress ... Get More Followers on [...]
Thank you thank you thank you thank you thank you!
In WordPress, you'll want to leverage number_format_i18n() instead, especially in distributed plugins or themes, as then it'll play nicely on blogs in other languages.
Hi, Andrew. As a core wordpress developer, I'm not going to disagree with you (especially as I only learned about number_format after a lot of googling!).
Is the difference that the the _i18n() version works out where you are and adjusts your commas and full stops accordingly?
Hey Malcolm, yeah, exactly. In terms of working out where you are, it bases it on the blog's language setting, not the reader's browser setting or anything like that.
A couple of things to make this work.
1. The code you give in the sidebar.php is get_follower_count but the function in functions.php is get_twitter_count. You need to make both of these the same (doesn't matter what it's called but they need to match)
2. The code in the blog (at least in IE9) isn't showing the correct syntax for some versions of PHP. It should be . Note that it starts with ""
Okay, obviously the comment engine strips out the PHP code. All PHP brackets need the ? symbol after the less then sign and before the greater than sign.
Hi, Bil. Thanks for the comments - I had some trouble copying and pasting the code and getting it to show up here! I've fixed all that now.
Next job: go and read about the number_format_i18n() recommendation that Andrew made. (Everyone else: just use it!)
Hmm. It now says 0 followers! It's been working fine all year until now ...