Update: 20-02-2010
Twitter Stream has now been updated. Support has been added back for parameter style calls to the function, twitter_stream(option, option, option) however you are strongly advised to change to the array/query string method which you can use by calling the wrapper function twitter_stream_args();. Some features like retweets & showing followers are not available through the old parameter method due to coding limitations & are only usable through the twitter_stream_args() function. You can find more on how to use the new function here.
I haven’t been able to test ReTweet support as much as I’d have liked so please report any bugs you find so I can fix them ASAP. Also those using WP-Super-Cache should update to 1.9 as soon as you can as I have fixed a huge problem which caused WP-Super-Cache to stop allowing Twitter Stream to update it cache.
Original Post
It’s been just over a year now since I released my first plugin which you may all know is The Attached Image, so I thought it was time I released another plugin. I hadn’t actually gone out on purpose to make this plugin, but a client’s website needed a simple Twitter feed plugin & while there are already WordPress plugins out there that do the job they insisted a custom one be built. So I set about making it & rather than have it sit in my WordPress functions folder doing nothing (other than working on the client’s site, of course) I thought I should release it as a plugin for anyone to use.
Althought the client requested the code be made I still retain the rights to use it as I wish. Just in case anyone was worrying about the useage rights. That means the plugin is under GNU (GPL) license as required by WordPress’s plugin repository.
Remember it is a very simple Twitter plugin, it is designed to do one thing and one thing only, show a user’s Twitter stream. Anyway here is some usage information.
What is Twitter Stream?
Twitter Stream is a very simple plugin originally created as a small function for a client’s website. Rather than have it sit doing nothing in my WordPress functions folder I thought I would extend it and make it into a plugin for everyone to use.
It’s functionality is very, very simple. It is designed to simply grab a set amount of status updates from the Twitter user account you request. If there are enough requests I may add some additional features, but for now it is designed to be very simple and run as quickly as possible while using the least amount of resources possible.
What Features Are There?
- Show the twitter timeline for any public username.
- Choose how many tweets to show.
- A Widget or template function is available.
- File caching to stop API overuse.
- Optional date shown in xx ago format, also links to permalink for the tweet. (Requested by Ron)
- Customizeable via CSS. (see the ‘Can I Style It?’ section below)
- Authentication for better API limiting & ability for protected users to show their tweets.
- @replies now link to the user profile of the user you are replying to.
- #tags now link to the Twitter search page for that hash tag.
- Link to user’s profile, customizable via CSS & via function parameter.
- Follower count can now be shown.
- Retweets can now be shown with normal tweets.
How Do I Install It?
You can download it from the WordPress repository here, and then install it as you would any old style WordPress plugin, which is as follows:
- Download the zip file and unzip it to somewhere, say your desktop.
- With any good SFTP client upload the extracted folder to your
wp-content/plugins/folder. - Enable the plugin in the WordPress plugin page.
Of course you can also install it by using the WordPress auto installer.
How Do I Use It?
Well because it is so simple there wasn’t really a point in making a admin page for two options. So there is a widget available for all you widget fans (WordPress 2.8 or later is required though), or if you’re a good ol’ fashioned function user you can also call the plugin by using twitter_stream_args();. To pass options you can either use a query string or an array (preferred) like so:
twitter_stream_args(array('username' => 'twitteruser', 'password' => 'password', 'retweets' => FALSE));
or
twitter_stream_args('username=twitteruser&password=password&retweets=false');
You can find a list of all the available options here.
What Are The Connection Methods?
Twitter Stream features three different connection methods due to problems with certain server configurations. The preferable way to connect is CURL, however it may not be installed on your server. Twitter Stream will try to connect try via CURL by default, if CURL is not available it will try the next method which is fopen. fopen is generally always available, however some servers have URL access via fopen turned off for security purposes. If both CURL & fopen aren’t available it will try socket access. This method nearly always works, if Twitter Stream still can’t connect then your server is locked up tight & probably doesn’t allow external file access at all (highly unlikely). However feel free to contact me & I’ll try my best to help should this occur.
How Do You Get Round The API Limit?
Twitter has a built in API limit that stops a single IP from abusing the service. It is approximately 150 requests per hour from a single IP address, 20,000 if your IP is whitelisted by Twitter (you must make a request to twitter for whitelisting). If you exceed that too many times you may be blacklisted from the Twitter API, which isn’t good. To get round this Twitter Stream has a cache system that asks twitter for the users timeline once every 30 minutes & stores it locally. If you aren’t happy with 30 minutes you can change it. If you open the plugin file and find line 159 $thirtyago = time() - 1800; you can change 1800 to any amount time in seconds. Just remember that if you lower it too far & you get blacklisted due to too many requests, I am not responsible. Finally, lowering the time too far will cause the cache to be renewed far quicker, this can cause undue stress on the server.
What About Shared Servers & The API Limit?
This is a massive problem. If you are on a shared server there is a huge chance someone else may have put your servers IP, which you all share, over the API limit. I’ve added a new error message that will tell you if you have exceeded the API limit, if this is the case & you are on a shared server, there is a way around it. I have added user authenication into the plugin. This means if you give your Twitter username & password it will log in to Twitter when it grabs the tweets, this changes the API limit from the servers IP to your accounts API limit meaning no sharing. Please bare with me though as this is all theory & I have been unable to test it however with some basic testing I have performed the API requests left when authenticated are different to the API requests left when not authenticated meaning this should work.
Sometimes I Get An Error Message!
There is a built in error message that is shown if Twitter fails to provide a vaild XML file. Reasons for this could be that the Twitter API is down (which Twitter claim is highly unlikely), or the plugin failed to read the cache file. If this happens, and you know Twitter is available, try deleting the cache file which is named username.cache. It is located in the same folder as the plugin, that’s generally wp-content/plugins/twitter-stream/username.cache. If you continute to get the error please contact me & I’ll try and help out.
Sometimes The Date Is -xx Seconds Ago?
This is sometimes difficult to get your head around. Simply put it is caused by your servers clock being faster or slower than Twitters clock. To work out the time ago it takes the timestamp (number of seconds since Unix Epoc) and takes it away from the timestamp at which the tweet was posted. If your servers clock is slower than Twitters, for the first few seconds it ends up giving a negative value therfore seeming as if it was posted in the future. It could be something completely different involving Aliens, badgers and evil pidgeons, but that is the most sensible thing I’ve been able to come up with. If you have a better idea what it is let me know.
Can I Style It?
You can. I haven’t added any styles so I could keep the plugin on one file & keep it free of clutter. The available CSS classes are:
.at-replyfor @replys..hash-tagfor #tags.a.twitter-linkfor autolinked URL’s within the timeline.a:hover.twitter-linkfor autolinked URL’s within the timeline when they are hovered over.a.twitter-datefor the date permalink.a:hover.twitter-datefor the date permalink when it’s hovered over..profile-linkfor the newly added link to user profile..follower-countfor the newly added follower count.
Can I Have A Options List?
Sure. The basic usage for in the template is this.
twitter_stream_args( [query-string/array] $args);
Here is the info for each parameter.
usernameshould be a string and is the username of the Twitter timeline you want to return. Required.countshould be a string and is how many tweets to return. Default ’10′.datebool or string. If set to TRUE it will show the time ago with a hyphen as a default separator. If it is a string of anything other than ‘TRUE’, ‘true’ or ’1′ it will use that as a separator.passwordyour twitter password as a string. Needed for retweet support. If you do not enter your password & enable retweet support it will just ignore it & show only normal tweets.profile_linka string. Allows you to customize what the link to your Twitter profile (shown at the bottom of the tweets) says. ‘Visit My Profile’ by default. Use a space, like this ‘ ‘, to show nothing.retweetsbool, true or false. Shows retweets with normal tweets. If you haven’t entered your password this value will be ignored.show_followersbool, true or false. Shows the number of followers.
Here is an example using all parameters.
twitter_stream_args(array( 'username' => 'twitteruser', 'count' => '10', 'date' => FALSE, 'password' => 'password', 'profile_link' => 'Visit My Profile', 'retweets' => FALSE, 'show_followers' => FALSE ));
I Need A Translation!
You can download translations files for the plugin here. These are .po & .mo files in a zip for the languages available. For the translation to work you only need the .mo file placed in the same folder as the plugin.
- Swedish Translation – Thanks to Albert Johansson
- Belarusian Translation – Thanks to FatCow
- French Translation – Thanks to Tolingo.com
I have provided the .po file for those who would like to amend or alter anything. Should you do that please send me a copy of the amended .po file so I can update the files here. If you would like to translate Twitter Stream into a language not available here you can download a blank .po file here. It is marked with Swedish codes but it is actually blank. Once you have filled that in send me a copy of the .po file & I will create the .mo file and offer it for download here. I will automatically credit you, but should you not want to be credited please tell me so in the email. You can email .po files to pablorobinson [at] gmail [dot] com.
A huge thank you to those who have translated so far & a big thank you to anyone who may help in the future.
I Have A Feature Request! / I’ve Found A Bug!
You can report all bugs via the comments which I read as soon as I can, or you can send me an email via the contact page. Either way I promise to try and get back to you as soon as possible.
Want To See A Working Demo?
Yep. If you want to see the Twitter Stream up & working, visit Medibolism and scroll down to the footer, it’s in the brown part on the right.
Where Do I Get It Again?
Once again you can download it from the WordPress plugin repository here, or you can install it via the WordPress auto installer in your blog admin.
I Love It, Can I Give You Some Love?
Wait… What?!! Oh you mean that sort of love. Sure.
You can help out in one of three ways. You can donate to help cover server costs & the cost of the coffee that keeps me coding. You can do that by clicking the button below.
You can buy me a present from my Amazon Wish List which would be very much appreated, or you can simply spread the word about Twitter Stream & The Attached Image.
Well that’s about it, again this is designed to be a very, very simple plugin. As I’ve said if you find any bugs or have any requests let me know.
Note: Twitter bird artwork in header image created by lisa-marieart.com. Used with permission.
*We currently have £44 of the £90 needed to keep our server running.
Leave a comment
Latest Posts
We're Saying...
It's late here in the UK but it's the 30th so a big Happy Birthday to @Y_Strahovski loads of hugs 'n kisses, hope you have a great day. - 9 hours ago
Wow! Jeffster are awesome and @Y_Strahovski has one hell of a throwing arm, lol. http://yfrog.us/n8b8xz (via @kentuckysocal) - 6 days ago
@AshBob87 No worries. Glad I could help out. ;) - 6 days ago
@AshBob87 No problem. I just found it a few seconds ago. Was sitting in my bookmarks all along. *facepalm* http://bit.ly/cI9cQS - 6 days ago
@AshBob87 Thanks. ;) I can't find the tutorial I followed anymore. :( - 6 days ago

Discussion: 113 Comments
If you had to change https to http it means your server does not have support for secure http.
Please do not use your password as it will be sent in plain text by the server & could be very easily be intercepted.
Has there been a recent change on your plugin or the Twitter side of things that changed the tweet listing to reverse chronological?
Well I haven’t changed anything recently, but you are correct. My twitter timeline is also in reverse chronological order.
I have absolutely no idea why though as my test server is in the correct order running the same version of the plugin. If you need to fix it right now here is how to do it while I try & find out what is going on.
Use the WP plugin editor (or a text editor of choice) and find the lines:
if($combtweets === FALSE) { return FALSE; } krsort($combtweets); foreach($combtweets as $tweets) { $output .= $tweets; }change
krsort($combtweets)toksort($combtweets).That would probably be the fix I would use, but I need to find out why if I apply this fix to my test server it goes from chronological to reverse chronological while this server seems to go the opposite way… Very bizzare.
I’m open to suggestions if anyone has any on the problem.
Just to let you know I’ve now figured out what was causing the problem & it’s fixed in the update which you should be able to get now.
Paul,
Thanks for finding and fixing the issue so quickly. (And for the mention in the changelog!)
No problem. It was a very strange bug so if you hadn’t brought it to my attention I probably would never have known. So thank you.
First off all; thanks for making this great plugin!
I do however have run into a small problem
“Rate limit exceeded. Clients may not make more than 150 requests per hour.
Warning: krsort() expects parameter 1 to be array, null given in /mnt/web5/33/74/52018474/htdocs/dsa/wp-content/plugins/twitter-stream/twitter-stream.php on line 178
Warning: Invalid argument supplied for foreach() in /mnt/web5/33/74/52018474/htdocs/dsa/wp-content/plugins/twitter-stream/twitter-stream.php on line 179″
i have no idea what i did wrong? i just installed it…
i tried my personal twitter account and it seemed to work!
please help!
Hi Richard,
That error is exactly what it say’s. Your server has exceeded it’s allowed API requests per hour (that’s 150 for non-whitelisted IP’s).
Unfortunately that is a problem with Twitter only allowing that many requests per hour. I assume you are on a shared server so everyone on that servers Twitter requests are using the same 150 limit (since you are all on the same server and therefore have the same IP).
If you are happy to you can enter your password, this will allow Twitter to identify you & use your personal API requests, again that’s 150 per hour but only you will be using them, not the entire server. Refer to the post above for info on using your password.
Hope that helps.
Hi Paul,
i can not recall that i am on a shared server… although i do run multiple twitter accounts on the same computer… but for this website it’s my first time using the twitter stream plugin so it’s not being used on any other machine or anything..
Thanks for the quick response… hope it will work later on today!
Hi again Richard,
I’ve checked the website you have used when you commented & you are hosted on a server along with about 34,000 other websites (I used a reverse IP checker to find out).
This means that all those other websites are using your 150 twitter API requests. The only way around this is to `log in` using your password so that Twitter identifies you as a user instead of just a IP address. It will then use your 150 API requests instead of the servers.
I hope that made what I meant a little easier to understand.
Hey Paul,
thanks again for responding so quick
As you probably noticed i’m still a little new to this thing, so excuse me and thanks again for putting up with my unknowing!
I will try and hope it works!
thanks for all the effort!
Will respond if i figured it out
thanks again!
- Richard
No worries, we all start somewhere.
I won’t be around for the rest of tonight (GMT+1 time) so it’ll probably be tomorrow morning before I respond.
Hey Paul!
I tried filling it in all over again and it worked!!!!!
Thanks so much! This plug in is perfect!!!
- Richard
No problem, glad it worked for you.
I’ve saw lot of blog now that using this plugin so that it is easier for the visitor to follow the blog in twitter if he’she liked it.
I get this error once in a while when the plugin can’t connect to Twitter (not the plugin’s fault… appears when Twitter service is down):
Warning: krsort() expects parameter 1 to be array, null given in /home/fruity/public_html/wp-content/plugins/twitter-stream/twitter-stream.php on line 178
Warning: Invalid argument supplied for foreach() in /home/fruity/public_html/wp-content/plugins/twitter-stream/twitter-stream.php on line 179
The problem is, I wish the plugin would convert that to a nicer, user-friendly error message. The current error messages take up space and throw off the alignment on my site.
Hey there,
Sorry about that. It really should convert to a nice error & shouldn’t give that message, so there is obviously a bug there. I’ll give it a check over for you and update the plugin to sort it out.
I’ve updated the plugin to try & stop the ugly error messages. I can’t really test it though since Twitter send out a special error in their stream if that happens & I can’t get Twitter to send one on command, unfortunately.
If you can give it a try & let me know what happens the next time it happens, it would be most appreciated.
Thank you so much for the quick response!
Not a problem, I just hope it works okay.
Hi, following up on the previous issue… I’m still getting the same error messages after the update.
Sorry about the problems. I’m not exactly sure how to fix it. I’ve followed Twitters error checking guide which explains how to deal with errors sent back by Twitter.
I can only think of one more thing to try, I’ll update it now & hopefully it will work.
Ok, I’ve added one last idea I had to try & catch the errors. If this doesn’t work I’ll have to try to find a way to simulate an error so I can test it as I’m working blind at the moment.
Sorry for all the problems, hopefully that will fix it.
Hi,
I have submitted a comment here but it doesn’t appear. I think it goes to the spam folder. Please reconsider my comment.
Your codes are really fantastic. Can I also use it for my blog. I really want to have a Twitter stream on it to have more followers and visitors.
Hi! I just wanted to report that your fix (RE: nicer error message handling) WORKED! This now appears when Twitter is down:
Your Twitter stream could not be collected. Normally this is caused by no XML feed being returned. Why this happens is still unclear.
Ooop! Twitter seems to be down. Please either delete the cache file using FTP or wait until it auto clears.
THANK YOU for the fix!
Both those errors shouldn’t show at the same time so a little tweak needs to be done there, but at least the fix works.
Thank you for letting me know.
By the way, you probably already know this, but I thought I should tell just in case you didn’t know… re-tweets still don’t show up. For example, I set up my sidebar to show only 1 tweet. The re-tweet wasn’t fetched and displayed.
The cache file only contained these few lines:
a:1:{i:0;s:75:”
“;}
The array didn’t show. Try putting it in [ php ] [ /php ] tags (without the spaces of course).
I’m not having any trouble with ReTweets as you can see in my sidebar (yes the pinching Suzi Perry’s bum tweet, lol). If you can provide the array I’ll definitely have a look at if for you though, just it case there is a problem.
I know there is a problem where it will show one less than you tell it too. I haven’t been able to track down the cause of that bug. I am still working on it though.
Hi Paul,
Can I still use this even I already have facebook on my site? It is just like this.
Errrm… Facebook?! This plugin is for putting Twitter on your WordPress blog.
Hello!
still can’t get retweets, have entered password to twitter account and marked “show retweets” but still nothing from the retweets, they show up fine in my twitter account page
I’m not sure what is causing that. I can see retweets okay. There should be a retweet in the sidebar when the cache updates. I’ve also checked with a few friends sites & they show them okay too.
If you can give any more information that might help I’ll see if I can find any bugs. Just without the ability to replicate a problem it’s exceptionally hard to find the problem & fix it.
Ok my twitter account is ‘kthinnovation’ and the twitter widget on the page innovationspaviljongen.com shows my tweets, but not the retweets that show up in the account.
What information can I send you to help find problem?
I realized after I’d wrote that, that there isn’t really much else you could tell me to help.
Are you using the sidebar widget? My sidebar is using the template call (which you can see retweets are working on), so if you are I’ll check for bugs in the widget code later today as I don’t have access to my test copy at the moment.
I apologize Donnie, I’ve just found a bug that only effected retweets being shown when using the widget. Since I don’t use widgets I never noticed it.
I’ve fixed it and have uploaded it to the WordPress SVN so it should be available soon.
Hi Paul,
I have just tried your plugin on a fresh install of WP 3.0 and it is coming up with an error:
Warning: array_slice() expects parameter 1 to be array, boolean given in /home/blah/wp-content/plugins/twitter-stream/twitter-stream.php on line 295
I have tried with password and no password, same error, plus 2 different twitter accounts.
It looks like a symptom of a bigger problem as it is failing due to the $content passed to twitter_stream_compile_tweets() is empty.
The server is running php 5.2.8 and it has libxml if that helps?
Any ideas?
Cheers
Rob
Hi Rob.
I’m not sure what is going on there. I’m currently running WP 3.0 (you can see in my source code) and my Tweets are showing okay.
According to the error $content is a boolean which would look empty unless you use
var_dump()to debug it. The only reason $content would be a boolean would be if Twitter sent back an error & even then the error checking should catch it.I’ve been having problems getting the error checking working properly since Twitter doesn’t error very often & it’s hard to replicate a Twitter error, lol.
Is there any other information you can give such as are you using a widget, or the template function? Also do you have SimpleXML installed on the server?
Hi Paul,
I’m using the plugin as a widget in a side bar – a second side bar if that makes a difference? I am pretty sure SimpleXML is installed on the server. I’m not in the office at the moment, but will be back there tomorrow. I’ll have a deeper look to see what the problem is – most probably something setup wrong on my server or something.
Will keep you posted.
Cheers
Rob
can’t get it running…
Could not authenticate you
Ooop! Twitter seems to be down. Please either delete the cache file using FTP or wait until it auto clears.
It work previously…
Please advice..
It happens sometimes. Twitter have been having fairly large problems with the API as of late & have been running at a reduced rate.
My best advice is to delete the cache as the error message says and then refresh the page in your browser using a Ctrl+F5 .