I 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.
Pingback: designfloat.com
Pingback: A collection of stuff » Blog Archive » Lleva el registro de lectores de tu RSS y Twitter en WordPress