Skip to content

Display Feedburner Subscribers Count

The following code tackles common problems with the existing feedburner scripts, like caching, uses the proper API and has a backup in case Feedburner returns the ever popular ‘0 Subscribers‘.

Hat tip to @Rarst for some of the code used below :)

Paste in the following code into your theme functions.php or your plugin.

if( !function_exists( 'wpprogrammer_feedburner' ) ):
function wpprogrammer_feedburner( $feedname )
{
	$interval = 3600;
	$cache = get_option('wpprogrammer_feedburner');
	$url = 'https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' . urlencode($feedname);
 
	if ( false == $cache )
		$cache = array();
 
	// if first time request add placeholder and force update
	if ( !isset( $cache[$feedname][$field] ) ) {
		$cache[$feedname][$field] = NULL;
		$cache[$feedname]['updated_at'] = 0;
	}
 
	// if outdated
	if( $cache[$feedname]['updated_at'] < ( time() - $interval ) ) {
		// holds decoded JSON data in memory
		static $memorycache;
 
		if ( isset($memorycache[$feedname]) ) {
			$data = $memorycache[$feedname];
		}
		else {
			$data = wp_remote_retrieve_body(wp_remote_request($url));
			if( $data )
				$memorycache[$feedname] = $data;
		}
 
		if ( $data ) {
			$xml = new SimpleXMLElement( $data );
			$circulation = intval( $xml->feed->entry['circulation'] );
 
			if( $circulation != 0)
				$cache[$feedname]['circulation'] = $circulation;
			$cache[$feedname]['updated_at'] = time();
		}
		else {
			$cache[$feedname]['updated_at'] = time()+60;
		}
 
		update_option( 'wpprogrammer_feedburner', $cache );
	}
	return $cache[$feedname]['circulation'];
}
endif;

To display your subscriber count anywhere in your blog, add the following code

<?php echo wpprogrammer_feedburner( 'feedname' ) ?>

Categories: .

Comment Feed

2 Responses

  1. Nice job adapting my Twitter snippet! I actually was lazy to and still use older and clunky code for feedburner.

    One suggestion – don’t hardcode it for “circulation” field, make it more flexible (like Twitter snippet that can fetch bunch of different stuff). There are some more fields that can be fetched (“reach” and something else I don’t remember) .



Some HTML is OK

or, reply to this post via trackback.