Display Feedburner and Twitter Stats in your Thesis Theme

by Ashwin

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.

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  • http://twitter.com/thotsunlimited/status/3124698823 Ashwin

    Display Feedburner and Twitter Stats in your Thesis Theme:

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

  • http://twitter.com/diythemes/status/3129414994 DIYthemes

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

  • http://twitter.com/matthodder/status/3129691637 Matt Hodder

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

  • http://leumund.ch Christian Leu

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

  • http://twitter.com/pearsonified/status/3130778451 pearsonified

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

  • http://kestrelsaerie.us 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?

  • http://aa-chan.net/ aachan

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

  • http://twitter.com/asifanwar4seo/status/3134493900 Asif Anwar

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

  • http://twitter.com/grameensol/status/3134497408 Grameen Solutions

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

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

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

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

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

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

    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.

  • http://kestrelsaerie.us 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!

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

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

  • http://leumund.ch 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.

  • http://kestrelsaerie.us 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.

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

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

    Thanks

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

    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!

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

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

  • http://links.retiendra.com/?p=35 Les liens de retiendra.com » Blog Archive » links for 2009-08-05

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

  • http://twitter.com/jimsimmons_/status/3145048931 Jim Simmons

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

  • http://twitter.com/sebastianj/status/3155129525 Sebastian Johnsson

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

  • http://www.scottwyden.com 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?

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

    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?

  • http://kestrelsaerie.us 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.

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

    Ok :) I hope the updated code should work…

  • http://www.scottwyden.com scottwyden

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

  • http://kestrelsaerie.us 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 Subscribers

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

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

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

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

    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

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

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

  • http://kestrelsaerie.us 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.

  • http://www.scottwyden.com scottwyden

    Cache is enabled as well

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

    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.

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

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

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

    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!

  • http://kestrelsaerie.us 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!

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

    Great! Waiting to hear if everything goes well.

  • http://www.scottwyden.com scottwyden

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

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

    You are welcome! Glad that you made it work.

  • http://kestrelsaerie.us Kestrel

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

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

    great to know!! congrats :)

  • http://kestrelsaerie.us Kestrel

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

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

    great to know!! congrats :)

  • manavecplan

    Hi Ashwin,

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

  • https://thoughtsunlimited.net/blog Ashwin / Thoughts Unlimited

    Sorry not really sure about the code in Blogussion.

    Thanks.

  • http://themethesis.com/tutorials/47-customizations-for-the-thesis-theme/ 47 Customizations for the Thesis Theme

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

blog comments powered by Disqus

Previous post: Post Updates to Social Networking Sites using your E-Mail (Thanks Posterous!)

Next post: