Internet
How to
Newspapers
Twitter
SEO advice
Categories: How to, Internet, Latest, Twitter, Wordpress.

How to show the number of Twitter followers you have in WordPress

January 20, 2011 9 Comments

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

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.)

You might also like
  1. Breadcrumb navigation trails and WordPress
  2. Open Graph: let people FaceBook “like” your WordPress (or other) website
  3. UK newspaper Twitter followers: 1 August
  4. UK newspaper twitter followers up 17% from August to September
  5. WordPress and smart/curly quotes: turning them off

Share this post

Follow me on Facebook or Twitter

9 Comments »

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.