PHP and JavaScript Fallbacks for Your Public Feedburner Count

With the recent Feedburner service outage, many sites across the Web experienced severe drops in their Feedburner subscriber counts. Apparently, Google is requiring all Feedburner accounts to be transferred over to Google by the end of February. In the midst of this mass migration, chaotic subscriber data has been reported to include everything from dramatic count drops and fluctuating reach statistics to zero-count values and dreaded “N/A” subscriber-count errors. Obviously, displaying erroneous subscriber-count data on your site is not a good thing. Fortunately, there are several ways to ensure that this doesn’t happen.

Over at CSS Newbie, author Rob Glazebrook weighs in with an excellent point about covering your bases when displaying your Feedburner subscriber count. As explained in the article, Feedburner’s handy API makes it easy to tap your data and display your subscriber count on your blog. Despite its best intentions, however, Feedburner occasionally returns inaccurate data or even no data at all for the subscriber count. For those of us who care about the accuracy of our publicly displayed feed statistics, displaying information like this on your site is simply unacceptable:

Join N/A awesome subscribers!

..or perhaps even worse:

Join 0 awesome subscribers!

Ugh. Clearly not the way to leave a good impression and encourage visitors to subscribe to your feed. Fortunately, there are several ways to deal with this situation, including avoiding the public display of your subscriber count or even finding an alternative to Feedburner. Yet for those of us who insist on proudly displaying our subscriber numbers, there are a couple of good methods for providing a fallback for your public Feedburner subscriber count.

As with Rob and his site, I am also using Francesco Mapelli’s excellent Feed Count plugin* here at Perishable Press. The plugin is a snap and provides all the functionality needed for customized text-display of your subscriber count. With the plugin’s text output of your feed stats, it is possible to echo virtually any message or markup you wish to embellish with your data. Unfortunately it doesn’t do anything for you when the Feedburner number is unavailable or otherwise inaccurate.

*Editor’s note: original link removed because the site is reported as an ‘attack site’ in Google. For an alternate version check out DigWP.com

JavaScript and jQuery Fallback

The JavaScript solution provided by Rob looks like this:

$spans = document.getElementById('subbox').getElementsByTagName('span');
if($spans[4].innerHTML=="N/A" || $spans[4].innerHTML=="0") {
	$spans[4].innerHTML = "thousands of";
}

This snippet of JavaScript evaluates the Feedburner count value of the Feedcount plugin. If either “N/A” or “0” is detected for your subscriber count, the script replaces the erroneous value with “thousands of” instead.

Rob also provides a jQuery-based fallback method:

if($(".subscribers").html() == "N/A" || $(".subscribers").html() == "0") {
	$(".subscribers").html("thousands of");
}

..which basically does the same thing as the more elaborate JavaScript method, only in a more straightforward and elegant fashion. Be sure and see the original article for more information on either of these JavaScript techniques.

A Better Solution

Of course, relying on a client-side technique such as JavaScript for a fallback solution is less than an ideal solution. Unfortunately, a significant percentage of users are visiting your site without JavaScript. Many mobile devices do not have JavaScript, many public and corporate browsers disable JavaScript, and many security-minded individuals disable it on their personal machines.

A better approach is to use a server-side language such as PHP to craft a more reliable solution. By generating the proper feedcount value before the data is sent to the browser, we ensure that all visitors see the correct subscriber information, regardless of browsing device and whether or not JavaScript is available.

PHP Fallback

Here is the PHP script that I am using here at Perishable Press:

<?php function feedcount_fallback() {
	if(function_exists('fc_feedcount')) {
		ob_start();
		fc_feedcount();
		$count = ob_get_contents();
		ob_end_clean();
		if($count == 'N/A' || $count == '0') {
			echo 'many other';
		} else {
			echo $count;
		}
	} else {
		echo 'many other';
	}
} ?>

Place that code into your theme’s functions.php file and then replace the default Feed Count function call with this in your theme’s template file:

<p>Join <?php if (function_exists('feedcount_fallback')) feedcount_fallback(); ?> awesome subscribers!</p>

Once in place, this code will output one of the following messages, depending on availability of accurate Feedburner subscriber-count data:

  • “Join 243 awesome subscribers!” — outputs correct count value when available
  • “Join many other awesome subscribers!” — outputs alternate value when count value is returned as either “N/A” or “0”

How does this script work? Basically, we are using PHP’s output buffering functionality to capture the output value of the fc_feedcount() function and compare it to the two possible error values (“N/A” and “0”). When the error values are detected, the alternate, fallback message is displayed; otherwise, the function displays the actual count value. Plus, as an added bonus, the function covers your bases by outputting the fallback message in the event that the Feed Count plugin itself should fail. This method ensures proper subscriber count display without relying on the availability of JavaScript, accuracy of Feedburner data, or even the functionality of the Feed Count plugin!

Bonus Tip

Of course, there are many ways to use and modify this script to suit your needs. For example, if you are providing a subscriber-count verification link, you may want to do something like this instead:

<p>
	<a href="http://feeds.feedburner.com/~fc/your-feed" 
	   title="Verify my subscriber count via Feedburner" 
	   rel="nofollow">
		Join <?php 
			  if (function_exists('feedcount_fallback')) 
			  feedcount_fallback(); 
		     ?> 
		awesome subscribers!
	</a>
</p>

Live Long and Prosper

Thanks to Rob for writing his article on this topic. Without it, I never would have thought to share this technique. Also thanks to Francesco Mapelli for his awesome Feed Count plugin. And thanks to you for reading the article and maybe even sharing your thoughts with us (hint, hint!).. ;)

[January 2009 and seven days of heavy fog]