Want to display your Feedburner RSS subscriber numbers on your blog? And maybe to show off your Twitter follower count as well? Look no further, you can find how to do that in this post.
I'll show you the easy way (using buttons from a service). And I'll show you a better way. It requires tweaking some PHP-code and your theme/template files, but if you're used to edit your files already, this will be easy.
With the better, more advanced way, we'll be getting the subscriber and follower counts as plain text, and then have all the control how to display the numbers any way we like and use CSS to make it all look nice.
The Easiest Way to Show Your Feedburner Subscriber and Twitter Follower Counts
The easy way to display your RSS subscriber count is to use Feedburner's Feed Count chicklet, which looks like this:
If you want this default button, go to your Feedburner account, check the "Publicize" section and active the "FeedCount". You will find the code for the chicklet inside, customize the colors, etc.
The easy way to display your Twitter follower count is to use service like TwitterCounter. One of their buttons looks like this (you can change the colors):
If you like that, go to the TwitterCounter. Register if you haven't, and click on the "Buttons" link at the top menu...
Boring! I want more personalized look...
The readily available options are OK, but wouldn't it be great to have your own customized subscriber count and not to have these same buttons that anyone can add to their website?
I think so, so I wanted to find a better way.
To display the number and control the style for the counts, we have to be a bit more creative. We need to fetch the numbers via other means, get it as plain text to our blog and then control the way it's displayed ourselves, through CSS for example.
The PHP code below has been updated to utilize WordPress options as a "cache" for the follower and subscribers counts, as well as the API timers (to avoid calling API too much and get banned).
How To Show Feedburner Subscriber and Twitter Follower Count as Plain Text
The trick is to get the subscriber and follower count through the API (Application Programming Interface) of these services. I'll show you the scripts that get the RSS subscriber count from Feedburner and Twitter follower count as plain text.
This way, you can display the counts like any text and apply CSS styles to them anyway you like.
The information is read from Feedburner and Twitter APIs using a PHP script. In WordPress, we can place the code to the functions.php and then call the function whenever we want to display RSS subscriber count or the number of Twitter followers.
To use the codes directly in other PHP-files they need to be modified a bit, but in this case, the functions.php is the best place for this. If you want to use the same PHP-code in some other PHP-based platform, you can, but you probably have to tweak the code snippets a bit.
Because of usage limits in the API, we want to avoid connecting and checking the subscriber count or the Twitter followers every time someone visits a page where the numbers are shown, so we'll also work a cache for the numbers, so the counts are only updated every hour (or whatever time you set for the cache check)
Because there is PHP involved and tweaking functions.php on WordPress, this is a bit technical, but should be doable by anyone. For most themes, the functions.php should be relatively simple and you can just copy-paste the code I provide you to the file.
If you have purchased a premium WordPress theme, I'd suggest contacting support of the theme and ask them for an assistance on how to add the functions to your theme.
Disable PHP Error Messages
When you start to add custom PHP functions to WordPress, chances are there will be problems, e.g. API not answering. The scripts below have some error handling, but not everything.
To avoid PHP errors showing in the blog, add this to your wp-config.php:
// log php errors
// enable or disable public display of errors (use 'On' or 'Off')
@ini_set('display_errors','Off');I also recommend you set up internal PHP error monitoring. If you want to hack into it yourself, you should check this post on how to to monitor PHP errors. After you have that set up, you can also monitor PHP errors via WP Dashboard Widget.
Editing the functions.php
- the functions.php file must have <?php at the very beginning and ?> at the very bottom.
- <?php starts the file, no line breaks ("Enters"), spaces or anything before it
- no line breaks or anything after the ?> at the end of the file
- the //+text are comments and are recommended (to understand what the code does when you look at it later), but not required.
- broken functions.php can prevent you from accessing the Admin Dashboard on WordPress. If this happens, restore your backup or delete the functions.php if you don't know how to correct it
Before you do anything, backup your WordPress, and also take backup copies of your current theme, especially the functions.php and any file you will edit to display the counts in the blog (e.g. sidebar.php).
If your blog theme doesn't have functions.php, you can create it, by making a file like this:
<?php
// BEGIN: don't edit above
// Put all the code between the BEGIN & END comments
// END: don't edit below
?>
and save the file as functions.php. After you've added the code I show below, you can transfer the file to your theme directory.
Functions to Fetch Subscriber and Follower Count
I did the functions in a way that the counts are cached in WordPress options and only updated if an hour has passed from the last time. Feel free to increase the time if you want, it's not like we need to keep the number real-time.
- Add the three functions to the functions.php.
- The first one is common function, for cURL, which you can utilize on other functions than these as well.
- The second one is the function for getting the Feedburner subscriber count
- The third one is the function for getting the Twitter follower count
- EDIT all the parts needed: your domain, Feedburner feed name and Twitter user name.
The source code might be wrapped on multiple lines. It should be OK when you copy-paste the code. In general, either ";" or "{" starts a new line.
Function: cURL
cURL is our method of getting the data from the Feedburner and Twitter API. This function is used by the other two functions, so you want to have this in. It can be re-used by other functions that require cURL as well.
function curl($url) {
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_HEADER, 0);
// EDIT your domain to the next line:
curl_setopt($ch,CURLOPT_USERAGENT,"");
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$data = curl_exec($ch);
if (curl_errno($ch) !== 0 || curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
$data === false;
}
curl_close($ch);
return $data;
}
- Add your domain name between the "" on the line:
curl_setopt($ch,CURLOPT_USERAGENT,""); - e.g. mine would be:
curl_setopt($ch,CURLOPT_USERAGENT,"zemalf.com"); - NO http or http:// or such, just the domain name
Function: myfeeds_count
For this to work, you need to activate the "Awareness API" at your Feedburner account for the feed you're about to use for this. Go to your feed, check "Publicize" and activate "Awareness API". Done. the following code will take care of the rest.
// Get Feedburner RSS Subscriber count as plain text
add_option('myfeeds_count','0','','yes');
add_option('myfeeds_api_timer',mktime() - 10000,'','yes');
function myfeeds_count() {
$rsscount = get_option('myfeeds_count');
if ( get_option('myfeeds_api_timer') < (mktime() - 3600) ) {
// EDIT your Feedburner feed name here:
$fb_id = "";
$subscribers = curl("https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=" . $fb_id);
try {
$xml = new SimpleXmlElement($subscribers, LIBXML_NOCDATA);
if ($xml) {
$rsscount = (string) $xml->feed->entry['circulation'];
update_option('myfeeds_count', $rsscount);
}
} catch (Exception $e) { }
update_option('myfeeds_api_timer', mktime());
}
//Echo the count if we got it
if($rsscount == 'N/A' || $rsscount == '0') { echo 'many other'; }
else { echo $rsscount; }
}
- replace
$fb_id = "";with$fb_id = "your feed name"; - e.g. mine would be:
$fb_id = "Zemalf"; - as my feedburner feed is at http://feeds.feedburner.com/Zemalf
- Just the feeds name, not the whole URL = what comes after http://feeds.feedburner.com/
Note the precaution in case the API is unavailable, the function will show "many other" instead of number, to avoid you displaying text like "join my 0 subscribers!".
Both functions, the one above and the following one for twitter follower count use SimpleXML extension, which requires PHP 5. In case you run into problems with it, check the PHP version from your host. Good hosting services keep their softwares updated and PHP is essential for WordPress too, so this should be in order.
Function: mytwitter_followers
// Get Twitter Follower count as plain text
add_option('mytwitter_followers','0','','yes');
add_option('mytwitter_api_timer',mktime() - 10000,'','yes');
function mytwitter_followers() {
$twittercount = get_option('mytwitter_followers');
if ( get_option('mytwitter_api_timer') < (mktime() - 3600) ) {
// EDIT your Twitter user name here:
$twitter_id = "";
$followers = curl("http://twitter.com/users/show.xml?screen_name=" . $twitter_id);
try {
$xml = new SimpleXmlElement($followers, LIBXML_NOCDATA);
if ($xml) {
$twittercount = (string) $xml->followers_count;
update_option('mytwitter_followers', $twittercount);
}
} catch (Exception $e) { }
update_option('mytwitter_api_timer', mktime());
}
if ( $twittercount != '0' ) { echo $twittercount; }
else { echo "growing number of"; }
}
- replace
$twitter_id = "";with$twitter_id = "Your Twitter username"; - e.g. mine would be:
$twitter_id = "Zemalf"; - Don't add http://twitter.com or @, just the user name.
I didn't add similar precaution to Twitter function as I did for the Feedburner function, so in case the API is unavailable, this might return something funny.
Showing the Numbers in Your Blog
After you have created or updated the functions.php, EDITED the parts you need to edit and saved it in your themes directory on your server, the functions are ready to be used.
Display the subscriber count by adding this somewhere in your theme's php files:
<?php if (function_exists('myfeeds_count')) myfeeds_count(); ?>
Display the Twitter follower count by adding this to your theme's php files:
<?php if (function_exists('mytwitter_followers')) mytwitter_followers(); ?>
Or use them without the "function_exists":
<?php myfeeds_count(); ?>
<?php mytwitter_followers(); ?>
If you want to test that everything work, add them to footer.php with HTML comments to prevent anything being displayed to your readers, like this:
<!--
FEEDBURNER TEST: <?php myfeeds_count(); ?>
TWITTER TEST: <?php mytwitter_followers(); ?>
--!>
Then look at the source code of your blog, and check if you can see your subscriber and Twitter counts inside the comments you placed.
If you don't even see the text you added, the page is probably cached, by WP Super Cache -plugin for example, so check another page or temporarily disable cache for signed-in users.
If everything works as planned you should see the numbers in the source code, you can remove the test comment and add the function-calls to the files you want to use them in. As an example of how this works, my RSS subscriber count is on the sidebar.
The code snippets on this post were inspired by the PHP scripts on Perishable Press posts Stupid Twitter Tricks and PHP and JavaScript Fallbacks for Your Public Feedburner Count. I tweaked the code a bit, added the "cache" and modified the Twitter API call as well.
There you go, I hope this was useful to you!
Antti Kokkonen
Lt.Commander Worf of WordPress Hacks
Here's more cool posts similar to this one:
One link to this post.
34 comments.
When I switched my domain name and server recently my RSS list crashed and burned. I’m starting from scratch. This will be useful for when I hit a thousand RSS followers. Hopefully the world hasn’t come to an end by then.
.-= Gordie Rogers’s last blog ..Why Deadlines Are For Deadbeats. =-.
Mmm,lots of lovely techie tips. I display my RSS feed but have an ugly box. Now I know how to change that!
But when is they right time to display it? And why doesn’t everyone? This is the big dilemma:)
Annabel, that is an interesting question. It comes down to social proof. Some have said that showing numbers larger than 50-100 start to have an effect. The similar behaviour can be seen on the social network sharing. It is easier to re-tweet or Digg something that has been shared hundred times than something which hasn’t. Larger number “proves” to us that this is good content, it’s worth my time to read this if that many are reading already.
Showing low numbers might have negative effect, but I don’t think anyone has studied that. It would require split-testing the subscription rate using low numbers vs. high numbers. The problem with that is that you would be showing false numbers and I wouldn’t want to do that, but it would be an interesting social experiment.
Do you show your twitter number somewhere, I don’t see it?
.-= Dennis Edell’s last blog ..UPDATE! 4 Plugin Package – 75% Off! PLUS Something Extra From Me! :) =-.
At that time, no. Couldn’t find a good place for the count right away. But now the both counts are up with the follow me and RSS icon on the sidebar.
Yipes, it looks complicated. But maybe I could tinker with it for a while. My subscribers and followers count are not yet worthy of display. :-)
It looks more complicated than it actually is. If a WordPress theme already have functions.php, it’s a matter of copying the 3 functions to that file and then planning the PHP call where you want the count or counts to show. + You can always ask for a tech help to get it done if you’re unconfortable digging into the theme files yourself or just use the readily available buttons.
Wonderful post, I always like to show these two counters on all my blogs, worth the share, I will add your post to our facebook fan page :)
I think you are the coming on our interviews, stay tuned, I will publish it very soon Zemalf.
.-= Hesham @ FamousBloggers’s last blog ..Make Money Online The Way You Want =-.
I have my feedburner showing. But I don’t think I need to put my twitter followers hehe. There are only about 100+ followers. Kinda embarrassing hehehe.
.-= Rian’s last blog ..Gift Suggestion – Syma Radio Control Helicopter =-.
This is freakin sick! I have been looking forever to find someplace that shows you how to display Twitter Followers and RSS Subscribers in plain-text, it's really not that hard the way you display it.
This is freakin sick! I have been looking forever to find someplace that shows you how to display Twitter Followers and RSS Subscribers in plain-text, it's really not that hard the way you display it.
Hi, I've added your code to my blog. Twitter works fine, but feedburner return the following error in the browser:
” PHP Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in E:homemarcossimasWebstepbystepwp-contentthemesstreamline_blue_21functions.php:366 Stack trace: #0 E:homemarcossimasWebstepbystepwp-contentthemesstreamline_blue_21functions.php(366): SimpleXMLElement->__construct('', 16384) #1 E:homemarcossimasWebstepbystepwp-contentthemesstreamline_blue_21footer.php(79): myfeeds_count() #2 E:homemarcossimasWebstepbystepwp-includestheme.php(996): require_once('E:homemarcoss…') #3 E:homemarcossimasWebstepbystepwp-includestheme.php(974): load_template() #4 E:homemarcossimasWebstepbystepwp-includesgeneral-template.php(63): locate_template() #5 E:homemarcossimasWebstepbystepwp-contentthemesstreamline_blue_21index.php(89): get_footer() #6 E:homemarcossimasWebstepbystepwp-includestemplate-loader.php(61): include('E:homemarcoss…') #7 E:homemarcossimasWebstepbystepwp-blog-header.php(16): require_once('E:homemarcoss…') #8 E:homema in E:homemarcossimasWebstepbystepwp-contentthemesstreamline_blue_21functions.php on line 366 “.
I've already double-checked your instructions.
Can you give me some support?
I edited the error message out of your comment. I believe the Feedburner API has returned something else than XML = API didn't answer or was unavailable. I'll add a little error handling to the code (which I thought I did already).
Thanks for your quick response.
I replaced the code and things are better because now the error is treated and browser
doesn't return error and renders the html.
However, as myfeeds_count() returns N/A ('many others' string), I think I need more help..
i got a very good voilen wigdet…u may grab this from my blog…have a look…
http://drugtreatme.blogspot.com/
Fantastic post, Antti.
very detailed and easy to follow. This is a great help for those wanting to add those counters.
Fantastic post, Antti.
very detailed and easy to follow. This is a great help for those wanting to add those counters.
Fantastic post, Antti.
very detailed and easy to follow. This is a great help for those wanting to add those counters.
Fantastic post, Antti.
very detailed and easy to follow. This is a great help for those wanting to add those counters.
Fantastic post, Antti.
very detailed and easy to follow. This is a great help for those wanting to add those counters.
Fantastic post, Antti.
very detailed and easy to follow. This is a great help for those wanting to add those counters.
Hi, I managed to put this into my wordpress theme which i am testing on my virtual local server and i have php5 turned on but where i want the count to display instead it displays many other.
So i assumed that the script where u put the url of your domain could be the reason to why its not working so i changed it to your details to see if any difference was made and still showed up as many other.
I have my api active in feedburner so i am stumped as to why its not working…this might be a hard one but any ideas or has anything changed since then?
So you added “yourfeedname” between the quotations on, yes?
$fb_id = "";First thing to do is test whether or not the API is read correctly: feedburner.google.com/api/awareness/1.0/GetFeedData?uri=YOURFEEDNAME (You should see the XML file.)
Other than that it's pretty hard to debug without seeing the code.
I've the same error of Marek.
The code is the same of the post. I inserted my feed name and the url of the API with my id is correct but when I call the function, it's print only “many others” without number or “N/A”.
Works like a charm on my site…Anyway to style the color? Thanks
http://www.wolverinesocceracademy.com
Put the code inside a HTML tag and format with CSS, just like any other text/element in the page, for example:
<span class="feedcount"><?php myfeeds_count(); ?></span>.feedcount {color:red;}Thanks!
Thanks for the information long sought today applied in the case.
Hmm, I smell a spammer? Are you?
“SEO” doing comment spam is bad publicity, don't you know?
Hmm, I smell a spammer? Are you?
“SEO” doing comment spam is bad publicity, don't you know?
Thanks for sharing. But I am looking to show feedburner count in text.
So glad I found this. That big fat zero was getting on my nerves. Now I can replace the occasional zero with “Tons of”. :) I’ve got this working great. I used a plugin that lets me place php code in a text widget, so I can easily add the call on the sidebar via a widget. Love it. Many thanks!
I’m happy you got the subscriber count working :) My PHP code could use some work to make it even better, e.g. to counter the ups and downs in the count, but at least people won’t be seeing the zero :)
Right, I was thinking about how to deal with that as well, but the occasional low numbers are still a million times better than the zero.