How To: Twitter Bar, Popular Posts, Random Stats

Every week I receive more than a handful of emails from people asking me what I use to display my Twitter status on this blog, as well as how I do the "popular this month" block and display random blog stats in my sidebar. Hopefully this post will answer those questions.

Twitter Status

First off, Twitter has their own method for displaying your Twitter status on your website but they rely on Twitter-hosted scripts and generally hold up the page load. There is also Alex King's Twitter Tools Plugin for WordPress but the general consensus is that it's complex and takes customization to get it working like mine is.

As such, I use Dave Kellam's twitterRSS WordPress plugin. It's more of a hacky script than a plugin and last I checked, doesn't have any fancy admin panel page - but it works. It relies on the built-in RSS parser of WordPress which is used in the dashboard, so it will only update when that updates.. each ~1 hour. As Dave mentions, using it is as simple as a single PHP function call once the plugin is enabled. However, I like to expand on that so that your page doesn't get killed if you accidentally disable the plugin: <?php if(function_exists('get_twitterRSS')){get_twitterRSS();} ?>

Update: Mike Malone has added a few features to this plugin:

Popular Posts This Month

Instead of going by traffic for specific posts, the "popular posts this month" block in my sidebar goes by the number of comments on articles within the last month. I used the most commented plugin and adapted the SQL query to account for a time range. Here's the code:

<?php now = gmdate("Y-m-d H:i:s",time()); lastmonth = gmdate("Y-m-d H:i:s",gmmktime(date("H"), date("i"), date("s"), date("m")-1,date("d"),date("Y"))); popularposts = "SELECT ID, post_title, COUNT(wpdb->comments.comment_post_ID) AS 'stammy' FROM wpdb->posts, wpdb->comments WHERE comment_approved = '1' AND wpdb->posts.ID=wpdb->comments.comment_post_ID AND post_status = 'publish' AND post_date < 'now' AND post_date > 'lastmonth' AND comment_status = 'open' GROUP BY wpdb->comments.comment_post_ID ORDER BY stammy DESC LIMIT 6"; posts = wpdb->get_results(popularposts); popular = ''; if(posts){ foreach(posts as post){ post_title = stripslashes(post->post_title); guid = get_permalink(post->ID); popular .= '<li><a href="'.guid.'" title="'.post_title.'">'.post_title.'</a></li>'; } }echo popular; ?>

That code displays the 6 most popular posts. You can change the number of posts displayed by altering the number in the SQL query where it says "LIMIT 6".

Random Stats

This one became so popular and frequently requested that Mike Malone ended up making a WordPress plugin around it. My solution is rather simple: use rand() to create a random number and have a different action assigned to each number. I ended up using a bunch of else if's but in hindsight a switch-case statement might have been nicer.

The first section displays the total number of words blogged in the number of days your blog has been active. I hard coded the date I started my blog in there, so you'd have to change that. The next section displays the total number of posts and comments. The third section displays the number of spam comments killed by Akismet (I edited the Akismet plugin to remove CSS/images and only output a number). Finally, I have a link to but you can change that to anything really. The structure is fairly easy to grok even if you're not a super coder.

<?php var = rand(1,4); if(var==1){ function word_count(){global wpdb;now = gmdate("Y-m-d H:i:s",time());words = wpdb->get_results("SELECT post_content FROM wpdb->posts WHERE post_status = 'publish' AND post_date < 'now'");if(words){foreach(words as word){post = strip_tags(word->post_content);post = explode(' ', post);count = count(post);totalcount = count + oldcount;oldcount = totalcount;}}else{totalcount=0;}echo number_format(totalcount + titlecount);} word_count(); echo " words blogged in ".round((time() - strtotime('september 8 2005'))/60/60/24)." days"; } else if(var==2){ numposts = wpdb->get_var("SELECT COUNT() FROM wpdb->posts WHERE post_status = 'publish'");if (0 < numposts) numposts = number_format(numposts);numcomms = wpdb->get_var("SELECT COUNT() FROM wpdb->comments WHERE comment_approved = '1'");if (0 < numcomms) numcomms = number_format(numcomms); echo numposts . " posts and " . numcomms . " comments"; } else if(var==3 && function_exists('akismet_counter')){ akismet_counter(); echo " spam killed by Akismet"; } else if(var==4){echo "Can't spell my name? Use ";?><a href=""></a><?php } ?>

Any other requests?