New Bookstore! Save 20% on books with discount code: LAUNCH
Web Dev + WordPress + Security

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.

Feed Count Down

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 plugin1 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.

1 Editor’s note: plugin link removed because the site is reported as an “attack site” by Google. For an alternate version check out this post at 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 ]

Jeff Starr
About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
Digging Into WordPress: Take your WordPress skills to the next level.

8 responses to “PHP and JavaScript Fallbacks for Your Public Feedburner Count”

  1. Sweet i’ll add this to my things to add to the ultimate theme ;)

  2. Iacob Ionut - Freelance Website Design 2009/01/25 2:56 pm

    Ha … I used something like this one of my older sites , but instead of echoing “many other awesome…” I used ” Hundreds and counting … “.

  3. Have never used Feedburne ryet but thanks for sharing this because one day i might!

  4. You’ll never stop to surprise me, Jeff!!

  5. Jeff Starr

    @Donace: Hmmm.. funny, I happen to be working on a new theme as well ;)

    @Iacob: That’s just as good — I tried to keep it as unassuming as possible.

    @Vic: Sounds great — thanks for the comment!

    @Bleyder: I certainly hope not! :)

  6. you always are Jeff…always :p

  7. Tyler Abell 2009/12/11 1:07 pm

    If i wanted to include the php script on a page, such as my content page, and I have to post it trough the wordpress editor, how would I do that?
    Do I have to create some sort of short code and then in the editor inset [feed-count]

    Thanks

  8. Jeff Starr

    Hi Tyler, you may be able to rig something like that up with shortcodes, OR, and another option is to use the PHP-exec plugin (I think that’s what it’s called) to enable PHP functions directly in post content.

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
WP Themes In Depth: Build and sell awesome WordPress themes.
Thoughts
After 10 years working on my 2010 iMac, my upgrade finally arrived. Shiny new iMac shipped from Ireland :)
Too much caffeine weirds me out. But I love the taste of coffee. So once in a while I enjoy a small cup of decaf. Hits the spot.
Chris Coyier is a truly awesome person. One of the finest people I've ever worked with. Just #gottasayit
Excel won't open CSV file because SYLK format? Open it with text editor and add an apostrophe ' at the beginning of the file, save changes, done.
Displaying too many social media buttons and links all over the place imho makes you look desperate and frankly kinda sad.
Interesting and insightful domain security scanner. Could spend hours playing.
Seeing massive increases in spam and malicious attacks across the board. Nothing out of the ordinary, just a LOT more of it.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.