How To: Twitter Bar, Popular Posts, Random Stats

June 3, 2007 · 109 comments

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

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

<?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="http://pstam.com">PSTAM.com</a><?php } ?>

Any other requests?

{ 65 trackbacks }

kobak pont org » links for 2007-06-04
June 3, 2007 at 9:25 pm
links for 2007-06-05 « Tyrants & Tax Collectors
June 5, 2007 at 8:24 am
Dicas: Textos populares, Hotlink, Redirect » Aleatório | O lado randômico das coisas
June 27, 2007 at 2:44 pm
Christiaan Conover » Twitter Wordpress Plugins: Hit or Miss
October 22, 2007 at 11:56 pm
How To Display Your Twitter Status On A WordPress Blog
November 25, 2007 at 3:30 am
Adam Wilcox's WilcosWorld » Blog Archive » Twitter
February 29, 2008 at 5:05 am
40+ Most Wanted Wordpress Tricks and Hacks
June 15, 2008 at 6:53 am
40+ Most Wanted Wordpress Tricks and Hacks | 胡作非为 - 每天精彩文章推荐 - Best Articles of the Day
June 15, 2008 at 11:41 pm
30个优秀Wordpress技巧
June 16, 2008 at 9:51 am
30个优秀Wordpress技巧 | Yansen's Blog
June 16, 2008 at 10:07 am
30个优秀Wordpress技巧 | 半瓶可乐
June 16, 2008 at 11:02 am
用友资讯 » Blog Archive » 30个优秀Wordpress技巧
June 16, 2008 at 7:09 pm
WP.ORG.CN | 30个优秀的Wordpress技巧
June 16, 2008 at 9:05 pm
我要当站长–51dzz.com » 30个优秀Wordpress技巧
June 17, 2008 at 4:05 am
9月森林 » 40+ Most Wanted Wordpress Tricks and Hacks
June 18, 2008 at 9:58 pm
30个优秀Wordpress技巧 - 网络应用与资讯
June 19, 2008 at 4:18 am
40 consejos y trucos para Wordpress | aNieto2K
June 23, 2008 at 12:06 pm
Mas de 40 trucos y hackeos para Wordpress | Pboza
June 25, 2008 at 1:06 am
40+ Most Wanted WordPress Tricks and Hacks | Blogging Tips
June 27, 2008 at 2:41 am
40 Consejos para optimizar tu Wordpress » Comenta o Muere | Actualidad, Humor, Tecnología, Política, Ciencia, Música… y otros muchos temas de los que merece la pena hablar.
June 30, 2008 at 5:24 pm
vsFish » 30个优秀Wordpress技巧
July 2, 2008 at 12:45 pm
30个优秀的WordPress技巧 | 智康博客--个人日记|网络技术|免费分享|技术交流|WordPress学习
July 10, 2008 at 1:55 am
30个优秀的Wordpress技巧为您打造强大的博客 | abe design studio
July 11, 2008 at 2:23 am
你不能不知道的30个优秀Wordpress技巧 | Idiigo.com
July 14, 2008 at 7:53 am
谷歌客 » 30个优秀Wordpress技巧
July 15, 2008 at 9:18 am
Los 40 trucos mas importantes de Wordpress « Camyna.com
July 17, 2008 at 5:00 am
WordPress 常用技巧40+则,诚邀您的参与翻译 - WordPress中文站
August 2, 2008 at 3:17 am
25 Brilliant WordPress Tips, Tricks and Plugins to Make Your Blog Stand Out | Oragle
August 5, 2008 at 2:59 pm
韶华白首
August 7, 2008 at 4:53 am
30个优秀Wordpress技巧 | T.E.M.P
August 13, 2008 at 11:50 am
Wordpress Hacks, Guides and Tricks | TechnoBuzz.net
August 17, 2008 at 4:37 am
30个Wordpress技巧 | 灵度°CLEVER8
August 21, 2008 at 4:44 am
40 лучших хаков для Wordpress | Listeria.ru
August 21, 2008 at 5:46 pm
RCDM » Blog Archive » 40 consejos y trucos para Wordpress (muy interesante)
August 25, 2008 at 4:01 am
Redbots » 30个有用的Wordpress技巧
August 25, 2008 at 12:16 pm
WordPress 常用技巧40+则 : 从1981年2月开始的人生
August 30, 2008 at 11:51 am
Top 30 Most Wanted Wordpress Tricks and Hacks | Make Money Blogging Experiment
September 1, 2008 at 6:13 am
WordPress 常用技巧40+则 - 从1981年2月开始的人生
September 1, 2008 at 1:29 pm
冰水博客 » Blog Archive » 30个优秀Wordpress技巧
October 25, 2008 at 2:36 pm
41 Wordpress Tricks & Hacks | Tips For Web Users
November 4, 2008 at 7:01 pm
41个最值得应用的wordpress技巧工具 | 大眼博客---睁大眼睛看
January 3, 2009 at 5:16 am
cssframework » 40+最值得应用的WordPress技巧和工具
January 4, 2009 at 7:37 pm
40+最值得应用的WordPress技巧和工具 - EventID中文网
January 5, 2009 at 12:10 pm
流动人生 » Blog Archive » 40+最值得应用的WordPress技巧和工具
January 13, 2009 at 3:34 pm
Wordpress增强:40多个你最想要的Wordpress技巧和插件工具 : 互联网营销博客 - Internet Marketing
January 29, 2009 at 12:22 pm
Face The World » 40多个你最想要的Wordpress技巧和插件工具
February 8, 2009 at 8:42 am
Complete Wordpress Theme Tutorial « Kolmex
February 10, 2009 at 6:47 pm
40+提高WordPress博客应用体验的技巧/插件 - 菠菜博
February 17, 2009 at 11:44 pm
Wordpress Tricks and hacks of my choice | r_shahin : i am web designing
February 27, 2009 at 1:18 pm
wordpress實用技巧40招 | 時間不等於金錢
March 2, 2009 at 10:54 am
智康博客 » 30个优秀的WordPress技巧
March 4, 2009 at 8:42 pm
Jan Design Studio - Jan 设计工作室 » Blog Archive » wordpress实用技巧41则,附相应插件下载
March 14, 2009 at 1:45 pm
10 useful WordPress hacks for every blogger/designer
March 17, 2009 at 9:51 am
Display Random Stats on Wordpress Sidebar
March 30, 2009 at 10:42 pm
Wordpress Tips and Tricks | Theme Heven
April 19, 2009 at 6:47 am
我想网 » Blog Archive » 40+最值得应用的WordPress技巧和工具
April 21, 2009 at 1:01 am
Wordpress Tricks and Hacks « Online Free Applications Software Tips Tools Wallpapers
April 22, 2009 at 4:43 am
D.K推荐41个最有用的WordPress技巧 | Duo.Ka’Blog
April 24, 2009 at 12:00 am
بیش از 50 مرجع برای یادگیری و استفاده بهتر از وردپرس | لينكدوني
April 24, 2009 at 1:50 am
40+提高WordPress博客应用体验的技巧/插件 | BeckMint
May 15, 2009 at 11:06 pm
برگزار کننده مسابقات آنلاین بیش از 50 مرجع برای یادگیری و استفاده بهتر ا
May 16, 2009 at 11:48 am
Link FA | لینک فا » بیش از 50 مرجع برای یادگیری و استفاده بهتر از وردپرس
May 17, 2009 at 8:12 pm
40+最有用的WordPress技巧 - 米老虎的BLOG
May 18, 2009 at 12:07 am
40 most wanted tips and tricks for Wordpress
June 2, 2009 at 6:35 am
70 Very Useful Wordpress Hacks & Tricks » De Web Times - Sharing Useful Resources.
June 30, 2009 at 4:39 am

{ 44 comments… read them below or add one }

1 zzap June 3, 2007 at 7:35 am

That Twitter thing is handy. Thanks.

Reply   More from author

2 Marvin Sum June 3, 2007 at 8:25 am

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.

Reply   More from author

3 bandersnatch June 3, 2007 at 9:30 am

Thank you for sharing, Paul.

I’ll be incorporating some of these mods shortly.

Reply   More from author

4 Ryan Powell June 3, 2007 at 11:15 am

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

Reply   More from author

5 Richard Crowley June 3, 2007 at 11:41 am

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’));

Reply   More from author

6 Joseph Rosario December 17, 2008 at 6:48 am

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

Reply   More from author

7 Dimitry June 3, 2007 at 1:58 pm

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

Reply   More from author

8 Brian Heys June 3, 2007 at 2:55 pm

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.

Reply   More from author

9 Chris Marshall June 3, 2007 at 3:00 pm

You read my mind :-) Will reply to your emails in a day or so, should be ready to launch then!!!

Reply   More from author

10 Alex June 3, 2007 at 3:18 pm

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.

Hmm, first I’ve heard of it. Perhaps you can mail me a description of the issues.

Reply   More from author

11 Adam June 3, 2007 at 3:52 pm

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!

Reply   More from author

12 Jessie June 3, 2007 at 4:21 pm

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.

Reply   More from author

13 Chris Marshall June 3, 2007 at 4:27 pm

@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

Reply   More from author

14 Daniel Andrade June 3, 2007 at 4:45 pm

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!

Reply   More from author

15 Daniel Andrade June 3, 2007 at 4:46 pm

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]
?>

:)

Reply   More from author

16 Adam June 3, 2007 at 5:37 pm

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.

Reply   More from author

17 Jessie June 3, 2007 at 5:38 pm

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

Reply   More from author

18 outsider June 3, 2007 at 5:44 pm

thanks paul. Still some problem with the recent comment thing, but I’ll try to fix it.. thanks anyway

Reply   More from author

19 Paul Stamatiou June 3, 2007 at 5:45 pm

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

Reply   More from author

20 Paul Stamatiou June 3, 2007 at 5:46 pm

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

Reply   More from author

21 titanium_geek June 3, 2007 at 11:03 pm

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!

Reply   More from author

22 Paul Stamatiou June 3, 2007 at 11:07 pm

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

Reply   More from author

23 Mike Malone June 3, 2007 at 11:54 pm

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.

Reply   More from author

24 Paul Stamatiou June 4, 2007 at 12:01 am

Kickass Mike!

Reply   More from author

25 Micahville June 4, 2007 at 12:57 am

Thanks for the shout out to Mike, Paul. I implemented his plugin and discovered a great new blog. Gracias!

Reply   More from author

26 Micahville June 4, 2007 at 1:01 am

P.S. Offtopic, BUT you have to be 21 to rent a car in California. Have fun, and get a good one!

Reply   More from author

27 Adam June 4, 2007 at 4:24 am

Mike- well done on fixing that, it works, yay! Nice site by the way.

Paul- Many thanks for letting me troubleshoot via your website!

Reply   More from author

28 Astorg June 4, 2007 at 10:19 am

The Twitter function works in English, but not in French, because it renders accented characters as question marks.

Reply   More from author

29 Mike Malone June 4, 2007 at 11:16 am

@Adam & Michaville: Thanks for the props. I’m glad I could help!

Reply   More from author

30 Alex June 4, 2007 at 11:32 am

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

Reply   More from author

31 Blake Brannon June 4, 2007 at 11:38 am

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.

Reply   More from author

32 David Moore June 4, 2007 at 12:53 pm

“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

Reply   More from author

33 titanium_geek June 5, 2007 at 2:00 am

ooh ooh- I second Blake’s request for the css comment howto.

Reply   More from author

34 Adam June 5, 2007 at 9:33 am

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

Reply   More from author

35 georgeadams June 8, 2007 at 12:43 pm

“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

Reply   More from author

36 James Wong June 18, 2007 at 3:48 am

regarding twitterrss, which template should i insert “” to? i tried putting it somewhere on header.php but nothing shows up. pardon my noobity.

Reply

37 Sameer Sontakey August 29, 2007 at 3:45 am

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.

Reply   More from author

38 Zach Kollegger September 2, 2007 at 8:14 pm

dude, this things is awesome! it’s exactly what i was looking for..thanks!

Reply   More from author

39 Ralph Dagza September 8, 2007 at 2:56 am

can you teach me how to do this?

http://nixonnow.org/

i want to put my twitter stat on a non wordpress site

Reply   More from author

40 Stefaan November 1, 2007 at 3:23 pm

Great tip for the twitterRSS plugin but my tweets are not loading?

Reply

41 Daniel Andrade December 17, 2007 at 10:28 pm

It seems I’ve used your way now :)
Thanks

Reply   More from author

42 the constant skeptic January 28, 2008 at 9:24 pm

great post… I added the twitterrss to my site. thanks!

Reply   More from author

43 Jason February 29, 2008 at 12:00 pm

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!

Reply   More from author

44 Penny Butler August 23, 2008 at 3:11 pm

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> ";

Reply   More from author

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: How To: Lighttpd, PHP5 on TextDrive

Next post: iPhone on June 29th, But Not for Me