Twitter Followers Count Snippet for WordPress

Here’s a short snippet to grab and display your twitter followers count in WordPress. You can use this anywhere, sidebar, posts, header, footer. We’ll be firing an anonymous call to the Twitter API for a user profile. This method does not require authentication, unless you’re trying to view a protected profile. To make this slightly easier I’ve used the JSON functions which are available in PHP 5, but you can easily get them to work in PHP 4 (and as Alex mentioned in the comments below, WordPress comes with it’s own JSON functions for PHP 4 users, which is awesome).

I’ve modified the snippet a little bit due to comments below, and thank you again Alex (@Viper007Bond) for clarifying things with the WordPress HTTP API and those very useful Transients. I actually grabbed some ideas from Alex’s own version of the Twitter Followers Count for WordPress snippet and added a fail-safe route (for times when the Twitter API is down).

Anyways, the snippet now consists of a single function, which checks for a transient (for caching purposes), then fires a query to the Twitter API, and in case of an error return a stored followers count value from the past. In case the Twitter API responded fine, we set a new transient and store the value as the last successful. Here’s the new snippet:

// Use this function to retrieve the followers count
function my_followers_count($screen_name = 'kovshenin')
	$key = 'my_followers_count_' . $screen_name;

	// Let's see if we have a cached version
	$followers_count = get_transient($key);
	if ($followers_count !== false)
		return $followers_count;
		// If there's no cached version we ask Twitter
		$response = wp_remote_get("{$screen_name}");
		if (is_wp_error($response))
			// In case Twitter is down we return the last successful count
			return get_option($key);
			// If everything's okay, parse the body and json_decode it
			$json = json_decode(wp_remote_retrieve_body($response));
			$count = $json->followers_count;

			// Store the result in a transient, expires after 1 day
			// Also store it as the last successful using update_option
			set_transient($key, $count, 60*60*24);
			update_option($key, $count);
			return $count;

echo "I have " . my_followers_count('kovshenin') . " followers";

Yup, quite simple isn’t it? Now call my_followers_count whenever you need to retrieve your followers count ;) Hope this will be of use to someone ;) Cheers!

18 thoughts on “Twitter Followers Count Snippet for WordPress

  1. Nice article. I like the way you schedule an action for caching :). And I think you should make the twitter username is an argument of function instead of hard coded it.

  2. Nice and refreshing snippet. I had shared one I tinkered for Twitter count once and I still get questions about it. This functionality is kinda popular. :)

    Some things I want to note about your code:

    Instead of creating WP_HTTP object manually it's better to use something like this wp_remote_retrieve_body(wp_remote_request($url)); because it will handle the case when request fails without PHP warnings/errors.

    Nice approach with scheduling. Only one thing I dislike – we request value after we nuke value. If request fails we are left with nothing.

    This was very common situation for me with similar snippet for feedburner count. So I only overwrite previous value after I succesfully retrieved updated one, otherwise I just serve cached (and hope it will work better on next request). This is slightly more complex and requires additional timer ot time-to-update flag.

  3. Twitter Followers Count Snippet for WordPress «

  4. As Rarst mentioned, you should not be using the WP_HTTP class directly. The wrapper functions exist for a reason and will automatically load the file as needed.

    Read this:

    You should also be using a transient instead of an option and scheduling an event. Transients are options that have an expiry time.

    Read this:

    If you're interested, here's how I would write that function:

    • Hey Alex, nice job with the code. Although I'm not sure about using transients. My initial snippet could have gone good with transients, but I'm working on a more fail-safe function, as @Rarst mentioned it might be a good idea to check for a new count before nuking the old one, thus the option might be split in two, 'count' and 'last', perhaps an array. This will ensure that the 'last' value always contains something and we'll return it in case something's wrong with the Twitter API.

      I'm not too familiar with transients, but I guess that it'll be nuked after the time limit anyway, unless of course there's an easy way to run a check before the transient expires.

      As for the wrapper functions for WP_Http, you're right ;) I must have based my code on Ozh's snippet written in August 2009 ;)

  5. Twitter followers count (and much more) in WordPress |

Comments are closed.