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:
Update: Mike Malone has added a few features to this plugin: http://immike.net/scripts/twitterrss.txt.
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:
$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 PSTAM.com but you can change that to anything really. The structure is fairly easy to grok even if you’re not a super coder.
$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="http://pstam.com">PSTAM.com</a><?php } ?>
Any other requests?
Tweet This
Stumble This


{ 65 trackbacks }
{ 44 comments… read them below or add one }
That Twitter thing is handy. Thanks.
More from author
Yea, I’ve got one. If you asked Wordpress to display, say, 15 items per page, how do you modify the loop on your homepage so it displays a different number (say 10?)
Cheers Paul.
More from author
Thank you for sharing, Paul.
I’ll be incorporating some of these mods shortly.
More from author
I’ll have to try the twitter one out, I’ve been using the twitter scripts that def. tend to hold up my page load.
Thanks
More from author
Good stuff, Paul.
Is there any reason you don’t use strtotime() for things like getting last month? And is there any reason to use gmdate() instead of date()? Something like this would be my first thought:
$now = date(’Y-m-d H:i:s’);
$lastmonth = date(’Y-m-d H:i:s’, strtotime(’-1 month’));
More from author
Thanks for this tip! I was having an issue with this code on my blog.
It wouldn’t update like it was supposed to.
It works great now, only showing posts in the last month.
Thanks Richard (& Paul)!
-Joe
More from author
Popular this month is really cool. Really liked/like that addition
@Marvin Sum: There’s a variable you can put inside your index.php just before the loop, forgot what it is though. $Max_Posts or something of that sort, just check out the support WP forums.
Dimitry
More from author
I added Twitter to my blog’s header a couple of days ago, and am making use of a WordPress plugin called ‘SimpleTwitter’. So far, I’m pretty impressed with it. It was easy to install, and has a control panel facility, so no hacking around with PHP to incorporate your twitter ID, etc.
More from author
You read my mind :-) Will reply to your emails in a day or so, should be ready to launch then!!!
More from author
Hmm, first I’ve heard of it. Perhaps you can mail me a description of the issues.
More from author
Many thanks for that, I have been meaning to ask you how you did the twitter thing for a few weeks.
BTW, have you considered disabling the “nofollow” attribute for the comments on this page? Quite a lot of the regular commenters on this site would like to get to #2 (on Google) too!
More from author
Err this is gonna sound stupid but for that bit of code from Popular Post this Month, do I just insert the code wherever I want the popular posts to show up? Thanks.
More from author
@Jessie – I created a PHP enabled sidebar box and added the code into that, then positioned it where I want it on the sidebar.
‘Unfortunatley’ it is on the site I am developing but I also added it to http://www.almerimarlife.com
More from author
For twitter I used this code:
The only problem, is if there’s any problem with twitter feed, he rest of the site doesn’t load right.
Maybe I’ll change to your way!
Thanks for sharing this!
More from author
ops, here goes the code:
<?php
preg_match(’#(.*)#’,file_get_contents(’http://twitter.com/statuses/user_timeline/username.xml?count=1′), $matches);
echo $matches[0]
?>
:)
More from author
Has anyone actually managed to get the “hide_links” or “hide_username” bits working? Currently not matter what I set them as, it hides my links by doesn’t hide the username.
More from author
@Chris Marshall: Thanks. I was pretty sure that was it, but then I didn’t want to mess anything up. :)
And thanks for the post Paul. REALLY useful.
More from author
thanks paul. Still some problem with the recent comment thing, but I’ll try to fix it.. thanks anyway
More from author
@Richard – I’m not exactly sure why as I wrote it a long time ago, maybe I ran into some problem and tried the gmdate, or I just didn’t know that much about PHP when I wrote it.
@Daniel, I like your method but it still accesses twitter.com on the fly and will add another HTTP request. Although I don’t think it would be that hard to cache the XML for a few minutes.
More from author
@Alex King, personally I’ve only used Twitter Tools briefly but this is an example of the type of email others send me:
“I was just wondering which WordPress plugin you use to put your Twitter status on your blog. I’ve been using Twitter Tools but it doesn’t seem as customizable as yours looks.”
More from author
I don’t use wordpress… but I am sooo tempted to switch. :)
Awesome how to! the only minor quibble is that the code isn’t really formatted nicely- but not a real big issue as you can copy/paste it easily.
Thanks Paul!
More from author
@titanium_geek – that’s an issue I’ve been struggling with for a longtime. I’m trying to go about it without any plugins b/c I had a plugin I used all the time that stopped working and was no longer developed and I had to go back through my posts and change markup. I was considering using
tags but then the code doesn’t like line-wrapping – there is a way to force it but it’s not standards compliant.. I’ll have to dig deeper.
More from author
Great post Paul. I grabbed the twitter plugin and fixed a few things / added some features.
I added URL replacment, like twitter does on their site. So if you have a URL (like http://paulstamatiou.com) in your twitter it will be replaced with a hyperlink. I also added @reply replacment, so the username in an @reply links back to that user’s twitter page. Last, I added a trim function so you can trim your twitter to a particular length (I didn’t want mine wrapping onto another line).
@Adam: I also fixed a little bug with $hide_links, you may want to take a look (hint: replace $twitters with $twitter in the source). The “username replacement” is really just stripping whatever the name is at the beginning of your twitters. Mine isn’t my username, it’s my real name, so I put “Michael Malone” as my username and it works.
Finally, I commented a bunch of stuff so people can actually make some sense of the plugin without looking at the code. Adding an admin panel wouldn’t be hard, but I’ve got to pack so I can’t do it now… maybe some other time.
You can grab my new version here. Change the extension to .php when you put it on your server, of course.
More from author
Kickass Mike!
More from author
Thanks for the shout out to Mike, Paul. I implemented his plugin and discovered a great new blog. Gracias!
More from author
P.S. Offtopic, BUT you have to be 21 to rent a car in California. Have fun, and get a good one!
More from author
Mike- well done on fixing that, it works, yay! Nice site by the way.
Paul- Many thanks for letting me troubleshoot via your website!
More from author
The Twitter function works in English, but not in French, because it renders accented characters as question marks.
More from author
@Adam & Michaville: Thanks for the props. I’m glad I could help!
More from author
The 1.0 release of Twitter Tools has a template tag to show just the latest tweet:
http://alexking.org/projects/wordpress/readme?project=twitter-tools
More from author
Thanks Paul,
I have always wondered how to add twitter to my site without the ugly sidebar status they recommend.
Request
You should also write a how-to use the CSS bubbles for comments.
More from author
“Any other requests?”
Yes keep em coming! :)
Been waiting for you to write a post like this for a while its really useful! Top commenters would be could as a couple of plugins i tried for that didnt work.
Dave
More from author
ooh ooh- I second Blake’s request for the css comment howto.
More from author
@ Blake & titanium_geek: I thought I had seen Paul already write about this, but I can’t find it though the Archives. Either-way, he uses this bit of coding: http://www.willmayo.com/2007/02/10/css-speech-bubbles/
Hope that helps.
More from author
“Any other requests?â€
I’ ve got one! :) how can we show the post date bellow the post title in the “popular posts this month” thingie?
By the way, thanks for another great post!
George
More from author
regarding twitterrss, which template should i insert “” to? i tried putting it somewhere on header.php but nothing shows up. pardon my noobity.
Thanks for the tips! I’ve added the Twitter widget on my site as well. Only think that is kinda lame is that it take a while for it to refresh its content. I previously was using this method: http://remysharp.com/2007/05/18/add-twitter-to-your-blog-step-by-step/ but I like the one you use better since it works well with all browsers.
More from author
dude, this things is awesome! it’s exactly what i was looking for..thanks!
More from author
can you teach me how to do this?
http://nixonnow.org/
i want to put my twitter stat on a non wordpress site
More from author
Great tip for the twitterRSS plugin but my tweets are not loading?
It seems I’ve used your way now :)
Thanks
More from author
great post… I added the twitterrss to my site. thanks!
More from author
Hi, I love the Twitter plugin. I was wondering how to get it to spit out each “twitter post” in an ? I don’t really know much about PHP so I’m really confused.
Anyone know how to accomplish this? Thanks!
More from author
Hiya,
Great plugin, just wonderng how you got “Twitter:” to be linked to your twitter site?
It doesnt seem to like the a href.. but I can bold my “Twitter:” etc.. but gives a fatal error when I attempt to add:
// HTML or text to display before and after each twitter
$before = " <a href="http://www.twitter.com/happyches"><b>Twitter:</b></a> ";
More from author