Display Feedburner and Twitter Stats in your Thesis Theme

by Ashwin on August 4, 2009

in Blogging Tips, Thesis Tips

Got bored of the traditional Feedburner Feedcount image?  Stuck with the Twitter Plugins that claim to display the number of followers in your blog? Forget them all.  I will show you how to get the Feedburner and Twitter subscriber stats programmatically (using PHP) in your Thesis powered blog and display them at your convenience.  This can also be used on a Wordpress blog running any theme, with ease.

1.  Modify your custom_functions.php file

Let us first create a couple of functions in the custom_functions.php, that would do us the job of fetching the numbers.

You can find the custom_functions.php file under the following directory – Your Blog Theme Directory/custom/custom_functions.php

function get_feedburner_stats() {
  $fbrefreshtime = 43200;  //Refresh Feedburner twice in a day

  $fb = wp_cache_get('fbstats_key');

  if ($fb == false) {
    $yourfeeduri = 'ThoughtsUnlimited'; 
    $feed = 'https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri='.$yourfeeduri;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $feed);
    $feed = curl_exec($ch); curl_close($ch);
    $xml = new SimpleXMLElement($feed);
    $fb = $xml->feed->entry['circulation']; 

    //Set the Value in Cache
    wp_cache_set('fbstats_key', strval($fb), '', $fbrefreshtime);
  }
  return $fb;
} 

function get_twitter_stats() {
  $twitterrefreshtime = 54000;  //Refresh Twitter Stats once every 15 minutes

  $tw = wp_cache_get('twstats_key');

  if ($tw == false) {
    $yourtwitter = 'thotsunlimited';
    $twurl='http://twitter.com/statuses/user_timeline.xml?id='.$yourtwitter.'&count=1';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $twurl);
    $data = curl_exec($ch);
    curl_close($ch);
    $xml = new SimpleXMLElement($data);
    $tw = $xml->status->user->followers_count; 

    //Set the Value in Cache
    wp_cache_set('twstats_key', strval($tw), '', $twitterrefreshtime);  //Will expire after the Refresh time you set
  }
  return $tw;
} 

Make sure that you replace the $yourfeeduri and $yourtwitter values, with your own values

Updated on 08/05/2009:

Modified the Code to cache the numbers using WP_Cache, as excessive API calls might be unsuitable for blogs with many page views per hour. Thanks to Christian for pointing this out here.

Enable WP Caching, by adding define(‘WP_CACHE’, true); to your wp-config.php file. Check this out, for information on enabling the WP Cache

Make double sure that you enable the Awareness API for your Feedburner Account

feedburnertwittercount_1

2.  Call the function wherever you need and style the output

So now you have the way to get the numbers.  Call them from anywhere you need and appropriately style the output.  I would recommend using the Thesis OpenHook plug-in to place the output of these functions.

Calling the function is pretty straightforward. One example could be:

 Join the fleet of  Subscribers 

You can look at the sidebar of my blog, to see one other example in action.

Hope you found this useful. Add any useful extensions to this tip, as comments to this post.

  1. Show your latest Tweet on top of Content Column in Thesis Theme
  2. Thesis Theme : Setup Pagination on Home Page
  3. Create a Full-Height Sidebar and Full-Width Navigation Menu in Thesis Theme

Stay In The Loop!

 Subscribe to our RSS Feed
 Join on Twitter
 Get Posts via Email

  • Christian Leu
    i think you should add some caching of the data as twitter will not support too mutch api calls.
  • That's wonderful! I was puzzled when the Twitter stats was not showing up sometimes. I will think about a caching mechanism and update the post.

    Thanks for pointing out :)
  • Christian Leu
    i am saving the values and the time of last check in a wordpress option. like this i can check every 5 minutes for the twitter status and renew it in the option. for feedburner you can even read it out of the option more or less daily.
  • That sounds great! I am trying to use wp_cache functions to store these values. I will let you know how it goes.

    Thanks
  • i have an updated code using wp_cache and it seems to solve the problem. thanks for pointing out!
  • Kestrel
    I think this is a brilliant idea, but I've been working at it for a couple hours now, and it's simply not working on my site. I'm trying to include the calls in my multimedia box, but I've also tried other locations. I can get my RSS icon to appear, as well as the words "Subscribe and join" but it seems as soon as the php tries to execute (and yes, I've checked the OpenHook box to execute php *grin*), everything else stops, and my sidebars don't even display.

    I copied your custom.css code (tt_rss and tt_twitter styling), but that doesn't appear to do anything either. Any suggestions?
  • Ok. Can you make doubly sure that Awareness API is enabled for your Feedburner account (refer to my post). Even I had problems and later found this as the reason.

    Also can you try this on your browser address bar - https://feedburner.google.com/api/awareness/1.0...

    *Replace uri value with your Feedburner name*

    You get a blank page (atleast on my Chrome) and try to view the source. You should get something like this:







    With this we can be sure that there is no problem on Feedburner side. Please get back to me if this works. We can sort out the next problems, then.
  • Kestrel
    Double checked and still have the Awareness API enabled. And I got the same page you did with that link, so I guess the FeedBurner side is still working.

    For the time being, I have restored my original code to my Multimedia Box. I will also try to put your code there; it just seems to be a convenient place at the moment. However, as it's after midnight here, I probably won't have this done tonight, but I may give it one shot.

    Thanks for following up, Ashwin--the concept is great, and I hope we can get it to work!
  • Sure. Let me know how it goes and we will sort out problems if any. Good night!
  • Kestrel
    Tried again:
    1. Code copied and pasted directly to custom_functions.php and saved.
    2. Join the fleet of Subscribers copied and pasted as-is to the multimedia box.
    3. Everything after "Join the fleet of" is gone: sidebars and footer.
    4. Repeated (2) but pasted to the middle of my widgetized footer.
    5. Again, everything after "Join the fleet of" is gone: the Thesis ad, as well as the third footer area.

    Very, very puzzling. Obviously, other php code is working just fine. And now it's 1am; I'm off to sleep on this mystery.
  • Did you try my code as is. Actually there was an error caused by to migration.

    In the Twitter stats fetch:
    $twurl='http://twitter.com/statuses/user_timeline.xml?id='.$yourtwitter.'.'&count=1';

    Just check this.

    Also I am working on an improved version of code to cache results and limit excessive API usage. I will let you know soon!
  • Kestrel
    The only thing I changed in your code was thotsunlimited to kestrelsaerie in 2 places. :) Will give the updated code a shot. Trouble is, it was failing even before it got to the Twitter call.
  • Ok :) I hope the updated code should work...
  • I have updated the post fixing the problem of frequent API calls. Try this and see if it works!
  • Kestrel
    Unfortunately, same result. Copied your changed code exactly, pasted it into custom_functions.php. Here is what I put into OpenHook > Multimedia Box:

    Join the fleet of Subscribers

    Join the fleet of followers


    And here is a picture of the output:

    There should be a lot more below that (my sidebars) but it's completely blank.
  • I just tried the exact same code in my theme. It works like magic!

    I am using WP Cache to cache the numbers. Have you enabled WP Cache?
    Check this out -http://codex.wordpress.org/Function_Reference/WP_Cache

    You just have to add an entry in the wp_config.php file
  • Kestrel
    Yep, WP-Cache is enabled; I checked that as soon as I saw your modified code, and it was already enabled.

    The only thing I can figure is I have something going on elsewhere that is inhibiting some of the php code. Either that, or I wonder if my webhost is somehow interfering with some php execution: I occasionally get error messages from them when I install or modify plugins, for example.

    I'll keep working at it. It just seems strange that things just completely shut down as it tries to execute the php call.
  • That's how PHP works. If there is some error, it immediately aborts the rest of execution.

    I am wondering what could be the problem. Just check if all your WP folders have write permissions. That could also stand in the way of Caching sometimes.
  • When surfing, I found a similar code snippet that does the same thing without WP Cache. Can you try this out?
    http://www.blogussion.com/design/display-twitte...

    But again it is more or less the same code!
  • Kestrel
    Actually, I'm pretty sure I've found the problem: custom_functions.php has some poorly written code in the default version. There is a "?>" buried in the middle of an example that was screwing up everything after it. While I haven't had time to reinsert your code, I'm sure it's going to work fine when I get to it.

    Thanks again for your patience!
  • Great! Waiting to hear if everything goes well.
  • Kestrel
    SUCCESS! I think I even figured out a little bit about how to do some php coding.
  • great to know!! congrats :)
  • AA-CHAN
    This is perfect. I was looking at something like this myself. Thanks a lot for the tip.
  • Great to know that it helped you. Let me know if you need more! Btw, you have a very nice landing page and a blog!!
  • scottwyden
    I tried it and in the sidebar it won't load the numbers...in the single post it stops the sidebar from loading and cuts off the page.

    If I use it in the sidebar, should it take a while to grab the initial numbers?
  • At least I am not aware of any time lag. Are you trying both FB and Twitter numbers? Have you enabled Awareness API in your FB account?
  • scottwyden
    I'm trying both.. the FB Awareness is active too. Very odd
  • I am using WP Cache to cache the numbers. Have you enabled WP Cache?
    Check this out - http://codex.wordpress.org/Function_Reference/W...
  • scottwyden
    Cache is enabled as well
  • When surfing, I found a similar code snippet that does the same thing without WP Cache. Can you try this out?
    http://www.blogussion.com/design/display-twitte...
  • scottwyden
    Oddly enough it works but the Twitter one number is way off. Thanks for the tip though!
  • You are welcome! Glad that you made it work.
blog comments powered by Disqus

Previous post:

Next post: