Display Number of Twitter, Facebook and RSS Subscribers

Posted: December 27, 2012 in Uncategorized

As you begin to build a build of a subscriber, it may become desirable to start displaying your follower numbers from several avenues, such as your Twitter followers, Facebook fans or your RSS subscribers. It lends some credibility to your site, showing that you’ve already built a decent reader base.

I decided that it was about time that I did this on Do It With WordPress, for the experience (and this tutorial) if nothing else. It actually wasn’t as easy to come up with this information as I had hoped, so I ended up largely crafting these snippets myself to get it just the way I wanted.

Twitter followers

To display your Twitter followers as plain text in your theme, you first need to insert this code into your functionality plugin:

function diww_twitter_followers ($twitter_user) {

	 $url="http://twitter.com/users/show.xml?screen_name=". $twitter_user;
	 $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);
	 $tw_fol_count = $xml->followers_count;
	 if ($tw_fol_count == false) { echo '512'; }
	 else { echo number_format($tw_fol_count); }

}

There’s only one thing to configure in this snippet; sometimes retrieving the value will fail, so I’ve created a failsafe (512), which it will resort to in the event of not being to retrieve the actual number. You could either use an approximate number of followers, or create a little error message. It’s up to you.

Then to actually display the number in your theme, edit the theme file where the number needs to appear and insert the following snippet in the correct location, being sure to use the Twitter username that you’re actually interested in (instead of daclements):

<?php diww_twitter_followers ('daclements'); ?>

Facebook Fans

To get the number of Facebook fans from your Facebook fan page, we take a similar approach; initially, you need to paste the following snippet into your functionality plugin (thanks to Kevin Chard for the basis of this snippet):

// Get Facebook Fan Count //

function diww_fb_fan_count($fb_id){
         $count = get_transient('fan_count');
    if ($count !== false) return $count;
         $count = 0;
         $data = wp_remote_get('http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id='.$fb_id.'');
   if (is_wp_error($data)) {
         return 'Error getting number';
   }else{
         $count = strip_tags($data[body]);
   }
set_transient('fan_count', $count, 60*60*24); // 24 hour cache
echo $count;
}

Facebook Fan Page IDIn this instance, there’s nothing to edit in the snippet above. Once that’s in place, paste the following snippet wherever you want the count to appear. You’ll need to switch out the fan page number for your own page, which can be found in the URL of your Facebook fan page:

<?php diww_fb_fan_count ('154784051207520'); ?>

RSS (FeedBurner) Subscribers

If you run your RSS feed through FeedBurner, you can use this little snippet to show the number of subscribers you have. First of all, you need to go into FeedBurner and make sure that you have the Awareness API activated; without it, you won’t be able to get the subscriber count.

Once that’s handled, paste the following snippet into your functionality plugin:

// Get RSS Subscriber Count //

function diww_fb_count ($fb_user) {

	$fburl="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=". $fb_user;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL, $fburl);
	$stored = curl_exec($ch);
	curl_close($ch);
	$grid = new SimpleXMLElement($stored);
	$rsscount = $grid->feed->entry['circulation']+0;
	return number_format($rsscount);

}

function diww_fb_count_run($feed) {

	$fb_subs = diww_fb_count ($feed);
	$fb_option = "diww_fb_sub_value";
	$fb_subscount = get_option($fb_option);
	if (is_null($fb_subs)) { return $fb_subscount; }
	else {update_option($fb_option, $fb_subs); return $fb_subs;}

}

function diww_fb_sub_value($feed) {

	echo diww_fb_count_run($feed);

}

Then, in order to show off your subscriber count, just paste the following little snippet in the relevant theme file and change the FeedBurner ID (mine is wanderingbrit in the following snippet) so that you’re taking the number from your own RSS feed instead of mine:

<?php diww_fb_sub_value('wanderingbrit'); ?>

So, that’s it. Pretty simple huh? What other services would you be interested in getting such stats from? Would you find it beneficial to incorporate this into a plugin (I might consider it)?

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s