FeedBurner Back in November of 2007, I wrote how I added my customized feed widget with "Gettin' Jiggy wit It - WordPress Widgets, that is". Well, things have changed since then and I want a count of all 3 of my feeds combined. A WordPress plugin capable of doing it doesn't exist (yet).

I no longer use the Feed Count 1.2 Wordpress Plugin because it can only count ONE of my feeds. I have 3 feeds because I'm using both the WordPress FeedBurner FeedSmith plugin for my comments feed and the WordPress DualFeeds plugin for the other 2 feeds.

I recently discovered How To Get Your Feedburner Count As Plain Text purely by chance. It's a PHP5 script to pull a single feed count from FeedBurner. I changed it so it would query multiple feeds from FeedBurner, instead of just one.


First Things First

You have to enable the "Awareness API" for each of your feeds, or this won't work at all. It's on the "Publicize" tab at FeedBurner. The script I'm furnishing below is for PHP5 only. I could probably write it for PHP4, but I'll do it only if someone explicitly asks for it. Even so, I won't be in a rush to get it done. [Update: Someone did ask and it's here: A FeedBurner Plain Text Feed Count for All YOUR Feeds]

Step 1 - Fetch the Data

You can copy and paste this since I use the Unfancy Quote plugin to remove the fancy quotes put in place by WordPress. For "$feeds", make sure you enter the part after "http://feeds.feedburner.com/" for each of your feeds. You can reduce the array to 1 or increase it beyond 3 by editing that line. The "$path" line should be a path that you've created. I recommend a directory just off the root of your site.

<?php

$feeds = array('feed1', 'feed2', 'feed3');
$path = '/document_root/directory/filename.txt';

$fb = 0;
foreach ($feeds as $feed) {
$url="http://api.feedburner.com/awareness/1.0/GetFeedData?uri=$feed";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb += $xml->feed->entry['circulation'];
}
file_put_contents($path, $fb);
?>

Step 2 - Setup a Cron Job

If you have access to cron on at your web host, have it run the script like this:

php /document root/directory/scriptname.php

Of course, you need to use the full path to the script, not just the path from your website root directory. If you don't have access to cron, then you can use the service at Uptime (for host uptime monitoring) to query the script — you just have to follow their instructions. Uptime queries the URL to the script, not the server path.

I don't know where you are, so you have to decide when the best time to run the cron job or have Uptime do its thing. I noticed that FeedBurner finishes updating the data at about 6 am U.S. Eastern time right now, but it could change. It may be a good idea to run it at 7 am and 8 am U.S. Eastern time just to be sure.

Step 3 - Retrieve the Data

I use a standard text sidebar widget which allows PHP to be executed because I have the Exec-PHP WordPress plugin installed. If you have the plugin installed, you can include this code in a widget, a post or a page. If not, you can use it in any template file.

<?php
echo file_get_contents('/document_root/directory/filename.txt');
?>

That's it! You can surround the result in any way you see fit.

The Wrap-Up

Does this seem like too much work? It may look like it, but it really isn't. If you want to do this and you're having problems, feel free to comment, use the contact form, email me or even send me a Twitter tweet.

You can always use a FeedBurner chicklet for each single feed, but if you want two or more feed counts combined, you won't be able to get anything directly from FeedBurner that'll do it. By the way, the email subscriptions are included in the feeds, so you don't need that data through this code or any chicklet.