Display Feedburner and Twitter Stats in your Thesis Theme

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.


Share this post:

Post on Twitter
Share on Facebook

Thoughts Unlimited runs on Genesis Framework


Comments

  1. i think you should add some caching of the data as twitter will not support too mutch api calls.

  2. 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?

  3. This is perfect. I was looking at something like this myself. Thanks a lot for the tip.

  4. 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!!

  5. 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 :)

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

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

  8. Sure. Let me know how it goes and we will sort out problems if any. Good night!

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

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

  11. That sounds great! I am trying to use wp_cache functions to store these values. I will let you know how it goes.

    Thanks

  12. 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!

  13. I have updated the post fixing the problem of frequent API calls. Try this and see if it works!

  14. 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?

  15. 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?

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

  17. Ok :) I hope the updated code should work…

  18. I'm trying both.. the FB Awareness is active too. Very off.

  19. 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 Subscribers

    And here is a picture of the output. There should be a lot more below that (my sidebars) but it's completely blank.

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

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

  22. i have an updated code using wp_cache and it seems to solve the problem. thanks for pointing out!

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

  24. Cache is enabled as well

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

  26. 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…

  27. 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!

  28. 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!

  29. Great! Waiting to hear if everything goes well.

  30. Oddly enough it works but the Twitter one number is way off. Thanks for the tip though!

  31. You are welcome! Glad that you made it work.

  32. SUCCESS! I think I even figured out a little bit about how to do some php coding.

  33. great to know!! congrats :)

  34. SUCCESS! I think I even figured out a little bit about how to do some php coding.

  35. great to know!! congrats :)

  36. Hi Ashwin,

    Is this the same kind of code that Chris uses on Blogussion?

  37. Sorry not really sure about the code in Blogussion.

    Thanks.

Trackbacks

  1. Ashwin says:

    Display Feedburner and Twitter Stats in your Thesis Theme:

    Got bored of the traditional Feedburner Feedcount .. http://bit.ly/eKFQ9

    Reply
  2. DIYthemes says:

    RT @thotsunlimited: Display Feedburner and Twitter Stats in your Thesis Theme: http://bit.ly/eKFQ9 #thesiswp

    Reply
  3. Matt Hodder says:

    Display Feedburner and Twitter Stats in your Thesis Theme »http://bit.ly/APj3N ( also sweet theme ;) ) (via @thotsunlimited)

    Reply
  4. pearsonified says:

    This rocks: Display custom Feedburner and Twitter stats with Thesis! http://is.gd/22AbF #thesiswp

    Reply
  5. Asif Anwar says:

    Display Feedburner and Twitter Stats in your Thesis Theme » Thoughts Unlimited http://bit.ly/eKFQ9

    Reply
  6. Grameen Solutions says:

    Display Feedburner and Twitter Stats in your Thesis Theme » Thoughts Unlimited http://bit.ly/eKFQ9

    Reply
  7. Les liens de retiendra.com » Blog Archive » links for 2009-08-05 says:

    [...] Display Feedburner and Twitter Stats in your Thesis Theme » Thoughts Unlimited (tags: wordpress twitter feedburner) [...]

    Reply
  8. Jim Simmons says:

    Display Feedburner and Twitter Stats in your Thesis Theme … http://bit.ly/eKFQ9

    Reply
  9. Sebastian Johnsson says:

    Display Feedburner and Twitter Stats in your Thesis Theme Thoughts Unlimited http://bit.ly/1kQjLb #wordpress

    Reply
  10. 47 Customizations for the Thesis Theme says:

    [...] Display Feedburner and Twitter Stats in Your Thesis Theme Learn how to show your popuarity in your Thesis theme using custom functions. [...]

    Reply

Leave a Reply to scottwyden Cancel reply

*