Getting RSS and Twitter Subscriber Counts in WordPress

Mac AppStorm Subscribe SectionI previously wrote a post about how some blogs are displaying their RSS subscriber and Twitter follower counts. Mac AppStorm is combining their Twitter and RSS counts into one number, and FreelanceSwitch has a section in their footer with separate readouts for RSS, Twitter, and their podcast. Today I’d like to show you how to actually implement such a thing.

We’ll be using PHP and cURL to retrieve the numbers, and then caching them in the database with WordPress’s get_option() and update_option() functions, so we don’t slow things down or use-up your Twitter API limit.

First, here’s how you get your FeedBurner circulation as plain text:

$fburl="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=Webmaster-source";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $fburl);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb = $xml->feed->entry['circulation'];

Replace Webmaster-source with your FeedBurner ID (the part at the end of your RSS link) and you’re all set. Now you can echo out the $fb variable to display your RSS circulation.

To get your Twitter follower count, you would do something similar:

$twurl="http://twitter.com/statuses/user_timeline.xml?id=redwall_hp&count=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $twurl);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$tw = $xml->status->user->followers_count;

As with the FeedBurner snippet, replace redwall_hp with your Twitter username, and then you can just echo $tw to get your follower count. This snippet isn’t something you want to use as-is though. Twitter’s API limit would soon kick-in and it would quit working. It needs to be cached.

Now you have two code snippets that can retrieve the two statistics. Now they need to be added up into one number and cached.

Here’s the final function, which you can add to your WordPress theme’s functions.php file:

function my_subscriber_count() {

$subscribers = get_option('my_subscriber_count');

if ( $subscribers['cache_time'] < (mktime() - 3600) ) {

$fburl="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=Webmaster-source";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $fburl);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb = $xml->feed->entry['circulation'];

$twurl="http://twitter.com/statuses/user_timeline.xml?id=redwall_hp&count=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $twurl);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$tw = $xml->status->user->followers_count;

$subscribers['count'] = $fb+$tw;
$subscribers['cache_time'] = mktime();
update_option('my_subscriber_count', $subscribers);

}

echo $subscribers['count'];

}

Once you add that mess to functions.php, you can display the combined number in plain text simply by putting the following template tag anywhere in your theme:

<?php my_subscriber_count(); ?>

That wasn’t too hard, was it?

If you want to take the FreelanceSwitch route, with multiple counters, the code shouldn’t be too hard to adapt.

  • http://www.blogreign.com Wallace

    Nice tutorial, it would be perfect if you can custom a plugin for this subscriber tweak.

    • http://intensedebate.com/people/redwall_hp redwall_hp

      I *might* be able to do that sometime. I need to focus on my Tweetable plugin for the next couple weeks though. It's in the WLTC plugin competition, and the deadline for the final code is creeping forward. I need to make sure everything's as close to perfect as possible by then.

      I might be able to whip something together sometime, though.

  • http://40tech.com Evan

    Very nice. If my subscriber count keeps growing like it has been, I'll be adding this to my theme.

  • Pingback: designfloat.com

  • http://buddingbloggers.com Satish

    nice, but wont this create extra load on the server??
    This extra load on the server may increase the loading time.

    • http://ineeddiscipline.com/2008/07/04/19-blog-review-networks/ Dean Saliba

      I was wondering this as well.

    • http://intensedebate.com/people/redwall_hp redwall_hp

      If you use one of the first two code snippets, yes. It will call FeedBurner or Twitter on every page load, which isn't good. If you have a look at the last code snippet, you may notice that it has a caching mechanism. It will only call the services once every hour (3600 seconds).

  • Pingback: A collection of stuff » Blog Archive » Lleva el registro de lectores de tu RSS y Twitter en WordPress

  • http://www.andrewkeir.com Andrew Keir

    When adding this to my functions.php it makes all my pages load as white screens only, obviously i'm missing something obvious? any suggestions?

    • http://intensedebate.com/people/redwall_hp redwall_hp

      For starters, what are you adding to functions.php? Just the last snippet, the big one, right? Also, make sure your functions.php file has opening and closing PHP tags.

      • http://www.andrewkeir.com Andrew Keir

        Hi redwall,

        Yep just the last big snippet. I tried adding just the code, also a " <?php " before the snippet and " ?> " after it.

        My funtions.php seemed to be made up of 4-5 individual sections php captions ( thats the wrong terminology im sure, but you get what i mean? )

        it doesnt seem to have opening and closing tags like <html> blahblahblah </html>.

        is that wrong?

        • http://intensedebate.com/people/redwall_hp redwall_hp

          It doesn't need <html> and </html> tags, as functions.php is just code, not really HTML.

          Did you put the <?php my_subscriber_count(); ?> where you wanted the number to appear? (E.g. somewhere in the sidebar.php template.)

          • http://www.andrewkeir.com Andrew Keir

            yeah i realize it wouldn't use html, i meant that just to ask if their were php opening closing tags required, it seems not.

            I'm not getting up to the point of putting the <?php my_subscriber_count(); ?> anywhere. Just white screens as soon as i add the code to my functions file.

            im lost :(

          • http://intensedebate.com/people/redwall_hp redwall_hp

            It depends on how your functions.php file is set up. Tell you what, send me an email (see About/Contact page) and include your functions.php file. I can have a look at it.

  • http://looks.gd TONES

    How do I format the number with a comma in the thousand’s place?

    for example, if i had a subscriber count of 20000, i want it to show as:

    20,000

    thanks! great tutorial :D