How to show feedburner subscriber & Twitter follower counts

Code is PoetryWant to display your Feedburner RSS subscriber numbers on your blog? And maybe to show off your Twitter follower count as well? Look no further, you can find how to do that in this post.

I'll show you the easy way (using buttons from a service). And I'll show you a better way. It requires tweaking some PHP-code and your theme/template files, but if you're used to edit your files already, this will be easy.

With the better, more advanced way, we'll be getting the subscriber and follower counts as plain text, and then have all the control how to display the numbers any way we like and use CSS to make it all look nice.

The Easiest Way to Show Your Feedburner Subscriber and Twitter Follower Counts

The easy way to display your RSS subscriber count is to use Feedburner's Feed Count chicklet, which looks like this:

Subscribe to my RSS feed
If you want this default button, go to your Feedburner account, check the "Publicize" section and active the "FeedCount". You will find the code for the chicklet inside, customize the colors, etc.

The easy way to display your Twitter follower count is to use service like TwitterCounter. One of their buttons looks like this (you can change the colors):

If you like that, go to the TwitterCounter. Register if you haven't, and click on the "Buttons" link at the top menu...

Boring! I want more personalized look...

The readily available options are OK, but wouldn't it be great to have your own customized subscriber count and not to have these same buttons that anyone can add to their website?

I think so, so I wanted to find a better way.

To display the number and control the style for the counts, we have to be a bit more creative. We need to fetch the numbers via other means, get it as plain text to our blog and then control the way it's displayed ourselves, through CSS for example.

The PHP code below has been updated to utilize WordPress options as a "cache" for the follower and subscribers counts, as well as the API timers (to avoid calling API too much and get banned).

How To Show Feedburner Subscriber and Twitter Follower Count as Plain Text

The trick is to get the subscriber and follower count through the API (Application Programming Interface) of these services. I'll show you the scripts that get the RSS subscriber count from Feedburner and Twitter follower count as plain text.

This way, you can display the counts like any text and apply CSS styles to them anyway you like.

The information is read from Feedburner and Twitter APIs using a PHP script. In WordPress, we can place the code to the functions.php and then call the function whenever we want to display RSS subscriber count or the number of Twitter followers.

To use the codes directly in other PHP-files they need to be modified a bit, but in this case, the functions.php is the best place for this. If you want to use the same PHP-code in some other PHP-based platform, you can, but you probably have to tweak the code snippets a bit.

Because of usage limits in the API, we want to avoid connecting and checking the subscriber count or the Twitter followers every time someone visits a page where the numbers are shown, so we'll also work a cache for the numbers, so the counts are only updated every hour (or whatever time you set for the cache check)

Because there is PHP involved and tweaking functions.php on WordPress, this is a bit technical, but should be doable by anyone. For most themes, the functions.php should be relatively simple and you can just copy-paste the code I provide you to the file.

If you have purchased a premium WordPress theme, I'd suggest contacting support of the theme and ask them for an assistance on how to add the functions to your theme.

Disable PHP Error Messages

When you start to add custom PHP functions to WordPress, chances are there will be problems, e.g. API not answering. The scripts below have some error handling, but not everything.

To avoid PHP errors showing in the blog, add this to your wp-config.php:

// log php errors
// enable or disable public display of errors (use 'On' or 'Off')

I also recommend you set up internal PHP error monitoring. If you want to hack into it yourself, you should check this post on how to to monitor PHP errors. After you have that set up, you can also monitor PHP errors via WP Dashboard Widget.

Editing the functions.php

  • the functions.php file must have <?php at the very beginning and ?> at the very bottom.
    • <?php starts the file, no line breaks ("Enters"), spaces or anything before it
    • no line breaks or anything after the ?> at the end of the file
  • the //+text are comments and are recommended (to understand what the code does when you look at it later), but not required.
  • broken functions.php can prevent you from accessing the Admin Dashboard on WordPress. If this happens, restore your backup or delete the functions.php if you don't know how to correct it

Before you do anything, backup your WordPress, and also take backup copies of your current theme, especially the functions.php and any file you will edit to display the counts in the blog (e.g. sidebar.php).

If your blog theme doesn't have functions.php, you can create it, by making a file like this:

// BEGIN: don't edit above
// Put all the code between the BEGIN & END comments
// END: don't edit below

and save the file as functions.php. After you've added the code I show below, you can transfer the file to your theme directory.

Functions to Fetch Subscriber and Follower Count

I did the functions in a way that the counts are cached in WordPress options and only updated if an hour has passed from the last time. Feel free to increase the time if you want, it's not like we need to keep the number real-time.

  • Add the three functions to the functions.php.
  • The first one is common function, for cURL, which you can utilize on other functions than these as well.
  • The second one is the function for getting the Feedburner subscriber count
  • The third one is the function for getting the Twitter follower count
  • EDIT all the parts needed: your domain, Feedburner feed name and Twitter user name.

The source code might be wrapped on multiple lines. It should be OK when you copy-paste the code. In general, either ";" or "{" starts a new line.

Function: cURL

cURL is our method of getting the data from the Feedburner and Twitter API. This function is used by the other two functions, so you want to have this in. It can be re-used by other functions that require cURL as well.

function curl($url) {
    $ch = curl_init($url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_HEADER, 0);
    // EDIT your domain to the next line:
    $data = curl_exec($ch);
    if (curl_errno($ch) !== 0 || curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
        $data === false;
    return $data;
  • Add your domain name between the "" on the line: curl_setopt($ch,CURLOPT_USERAGENT,"");
  • e.g. mine would be: curl_setopt($ch,CURLOPT_USERAGENT,"");
  • NO http or http:// or such, just the domain name

Function: myfeeds_count

For this to work, you need to activate the "Awareness API" at your Feedburner account for the feed you're about to use for this. Go to your feed, check "Publicize" and activate "Awareness API". Done. the following code will take care of the rest.

// Get Feedburner RSS Subscriber count as plain text
add_option('myfeeds_api_timer',mktime() - 10000,'','yes');
function myfeeds_count() {
    $rsscount = get_option('myfeeds_count');
    if ( get_option('myfeeds_api_timer') < (mktime() - 3600) ) {
        // EDIT your Feedburner feed name here:
        $fb_id = "";
        $subscribers = curl("" . $fb_id);
        try {
            $xml = new SimpleXmlElement($subscribers, LIBXML_NOCDATA);
            if ($xml) {
                $rsscount = (string) $xml->feed->entry['circulation'];
                update_option('myfeeds_count', $rsscount);
        } catch (Exception $e) { }
        update_option('myfeeds_api_timer', mktime());
    //Echo the count if we got it
    if($rsscount == 'N/A' || $rsscount == '0') { echo 'many other'; }
    else { echo $rsscount; }
  • replace $fb_id = ""; with $fb_id = "your feed name";
  • e.g. mine would be: $fb_id = "Zemalf";
  • as my feedburner feed is at
  • Just the feeds name, not the whole URL = what comes after

Note the precaution in case the API is unavailable, the function will show "many other" instead of number, to avoid you displaying text like "join my 0 subscribers!".

Both functions, the one above and the following one for twitter follower count use SimpleXML extension, which requires PHP 5. In case you run into problems with it, check the PHP version from your host. Good hosting services keep their softwares updated and PHP is essential for WordPress too, so this should be in order.

Function: mytwitter_followers

// Get Twitter Follower count as plain text
add_option('mytwitter_api_timer',mktime() - 10000,'','yes');
function mytwitter_followers() {
    $twittercount = get_option('mytwitter_followers');
    if ( get_option('mytwitter_api_timer') < (mktime() - 3600) ) {
        // EDIT your Twitter user name here:
        $twitter_id = "";
        $followers = curl("" . $twitter_id);
        try {
            $xml = new SimpleXmlElement($followers, LIBXML_NOCDATA);
            if ($xml) {
                $twittercount = (string) $xml->followers_count;
                update_option('mytwitter_followers', $twittercount);
        } catch (Exception $e) { }
        update_option('mytwitter_api_timer', mktime());
    if ( $twittercount != '0' ) { echo $twittercount; }
    else { echo "growing number of"; }
  • replace $twitter_id = ""; with $twitter_id = "Your Twitter username";
  • e.g. mine would be: $twitter_id = "Zemalf";
  • Don't add or @, just the user name.

I didn't add similar precaution to Twitter function as I did for the Feedburner function, so in case the API is unavailable, this might return something funny.

Showing the Numbers in Your Blog

After you have created or updated the functions.php, EDITED the parts you need to edit and saved it in your themes directory on your server, the functions are ready to be used.

Display the subscriber count by adding this somewhere in your theme's php files:

<?php if (function_exists('myfeeds_count')) myfeeds_count(); ?>

Display the Twitter follower count by adding this to your theme's php files:

<?php if (function_exists('mytwitter_followers')) mytwitter_followers(); ?>

Or use them without the "function_exists":

<?php myfeeds_count(); ?>
<?php mytwitter_followers(); ?>

If you want to test that everything work, add them to footer.php with HTML comments to prevent anything being displayed to your readers, like this:

FEEDBURNER TEST: <?php myfeeds_count(); ?>
TWITTER TEST: <?php mytwitter_followers(); ?>

Then look at the source code of your blog, and check if you can see your subscriber and Twitter counts inside the comments you placed.

If you don't even see the text you added, the page is probably cached, by WP Super Cache -plugin for example, so check another page or temporarily disable cache for signed-in users.

If everything works as planned you should see the numbers in the source code, you can remove the test comment and add the function-calls to the files you want to use them in. As an example of how this works, my RSS subscriber count is on the sidebar.

The code snippets on this post were inspired by the PHP scripts on Perishable Press posts Stupid Twitter Tricks and PHP and JavaScript Fallbacks for Your Public Feedburner Count. I tweaked the code a bit, added the "cache" and modified the Twitter API call as well.

There you go, I hope this was useful to you!

Antti Kokkonen

Lt.Commander Worf of WordPress Hacks

Posted by

Topic: Blogging
Tags: , , , , , ,

If you enjoyed this post, sign up for updates (it's free)

Feedback, questions and comments are also welcome on my Facebook page