WordPress Plugin: Twitter Stream
Twitter Stream is a very simple Twitter plugin designed to easily show your Twitter timeline on your website. It features @reply & #tag highlighting, ‘time ago’ dates & customiseable CSS. Now includes oAuth support.
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. ![]()
Latest Info – 31/10/2011
Added the ability to return tweets into a variable so they can be manipulated via PHP before being output. This option was requested by advanced users & any users who do not want to use it should not notice any difference after upgrading.
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?
- Connect to Twitter securely using oAuth. (required)
- 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?
Due to Twitter not having a solution for open source applications using oAuth yet, you must follow some slightly more complicated instructions to sign in via oAuth. Comprehensive instructions are provided in the plugin but here is a rough overview.
First you need to register a Twitter App, save the consumer keys you are provided with, then sign in with Twitter. As I said more detailed instructions are provided as you set up the plugin, including tips for what to enter when registering you app.
Finally you can show your tweets using a widget. The widget is a WP 2.8 & up compatible widget. If you are using a WP version lower than 2.8 you should really upgrade for security reasons if nothing else.
If you are a good ‘ol fashioned function user then you can call Twitter stream using:
twitter_stream($arg);
Providing either a keyed array or an argument list. All the parameters available are listed here.
How Do You Get Round The API Limit?
Now that oAuth has been implemented API requests will be counted against your personal account rather than your servers IP address meaning that shared servers should no longer cause a problem. However to reduce the possibility of overusing the API even further caching is still in use.
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. You can do that using the brand new ‘delete cache’ button which is shown on the bottom of the authorization page. Just click ‘Twitter Stream’ under the settings menu to find it.
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.
Sometimes oAuth Misses The Deny/Accept Page When I Press ‘Sign in with Twitter’?
This happens if you have already authorized Twitter Stream to be used with your Twitter account. Until you press the sign in button the plugin can’t tell if you have already given authorization or not. This generally happens if you have more than one blog using Twitter Stream with the same Twitter account. It’s perfectly normal though.
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( [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. No longer required. If omitted will show the user who authorized Twitter Stream’s timeline.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.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.show_followersbool, true or false. Shows the number of followers.cache_timeSet the length of time tweets are cached for.echoControls if the tweets should be echoed out to the page or returned as a SimpleXMLObject for manipulation via PHP. Defaults to TRUE, meaning echo tweets to page.
Here is an example using all parameters.
twitter_stream(array( 'username' => 'twitteruser', 'count' => '10', 'date' => FALSE, 'profile_link' => 'Visit My Profile', 'retweets' => FALSE, 'show_followers' => FALSE, 'cache_time' => 30, 'echo' => TRUE ));
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
- Dutch Translation – Thanks to Rene
- Alternative Dutch Translation – Thanks to Sander Van Vliet
- Spanish Translation – Thanks to inMotion Hosting
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. Once you’ve done the translation send it to me at admin [at] return-true [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.
I Want To See A Working Demo?
Okay. Well the Twitter timeline here on Return True (on the right) is created using Twitter Stream. You can also see it on Medibolism & on Lisa Marie Art’s website.
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 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.







Discussion: 348 Comments
Thank you for the quick answer! I did the first step but when i copy the twitter follow button code into the field all is fine but next to button appears this “>
This is twitter follow button code:
Thank you for your time…
p.s. The code is from official follow button generator of Twitter
http://twitter.com/about/resources/followbutton
Hmm. It looks like WordPress is stripping the HTML from it. Unfortunately I can’t avoid that, WordPress thinks HTML should be allowed to be submitted & is stripping it before saving. The only thing I can think of is to edit the plugin code (but you’ll lose your changes next update) or you could place it using the PHP method, which WordPress wouldn’t strip.
I’m very sorry it’s so awkward.
Ok i see! Thank you for your help! I thing this will be nice feature for the next update.
I’ll see what I can do, but no matter what I try WordPress always removes the HTML you input.
The only way I’ve been able to get it to accept it is via the PHP function.
In the section after authorization there’s a ? missing ->
What Do I Do Now?
The app has been working fine until recently. For some reason, I get the “Invalid / suspended application” error. Twitter.com shows this app is suspended. What can I do to fix this error and get the twitter stream working again?
That’s very strange Olen.
That means the application that Twitter Stream was authorized under has been suspended by Twitter. Not sure why that would be though.
The only way I know of fixing it is to tell Twitter Stream to delete it’s keys & unauthorize itself via the options page and then follow the authorization instructions again from the start.
In the meantime I’ll look into why your application may have been suspended.
I just upgraded the plugin to the latest version and I get this error message:
Warning: simplexml_load_string() expects parameter 1 to be string, array given in …/wp-content/plugins/twitter-stream/twitter-stream.php on line 400
followers
actually… sorry… nevermind…
i downgraded to the previous version (2.3.1) and still got the same error… i deactivated the plugin and then upgraded automatically to the newest version and voila… it works again…
carry on!
Hi Alexo,
There was a massive error with version 2.3.1 I’ve just fixed it and uploaded 2.3.2 so if it’s working there is every chance you got that version instead, lol.
Someone kindly emailed me to tell me about the problem, so I fixed it as soon as I could.
I just upgraded to the latest version and I am getting the error below. Not that much of a plugin guru& don’t know where to start
Fatal error: Cannot redeclare class OAuthException in public_html/wp-content/plugins/twitter-stream/twitteroauth/OAuth.php on line 8
Hi Phiri,
This was a common problem before. I believe it means your host has the PECL oAuth extension loaded in your PHP install. It looks like I forgot to put the fix back in from the last version (*facepalm*) I’ll get an updated version out now for you. Look out for it in the next 15-25 minutes in your WP dashboard.
Hi Paul,
Thank you.
The thing is I cannot access wp dashboad –I get the same error. A quick pick on the Web suggests deleting the specific plugin directory… is that okay?
I am afraid I am a bit of a newbie when it comes to plugins
Oh sorry. I completely forgot it would cause a site wide error. The easiest way is to delete the plugin folder and just reinstall the new version from the WP plugin repository via the dashboard.
I just tried doing that –deleted and installed via dashboard. I get the error below.
Fatal error: Cannot redeclare class OAuthSignatureMethod_HMAC_SHA1 in public_html/wp-content/plugins/twitter-stream/twitteroauth/OAuth.php on line 118
Hi Phiri,
Looks like your hosts PECL version of oAuth is more detailed than the last versions I had problems with. I’ve got one more idea. I’ll do the update now, look for it in about 15-25 minutes again.
Hi Paul,
Thank you for responding and helping. I’ll let you the outcome.
Hi Paul,
Super cool –it definitely works 100%
Excellent stuff.
Hello,
I have installed the plugin, and I also went through the developer API steps to get the consumer and secret keys. I entered these keys, and then a “sign in with Twitter” link appeared in the settings page. But, when I click the sign in link, it leads me to a stack of errors. I think the callback URL must have a problem, but I’m not sure how to find what’s wrong. Any advice would be appreciated.
The specific error is below, I can upload an image of the error stack if it would help…
Fatal error: Call to undefined function curl_init() in C:\wamp\www\wp-content\plugins\twitter-stream\twitteroauth\twitteroauth.php on line 199
Hi Daniel,
I’m afraid the bad new is that the error you are getting is thrown when you don’t have the cURL extension installed on your server. The odd thing is that there are alternative connection methods in the plugin that should be used if cURL doesn’t exist.
Thanks a lot! That was an easy fix, just enable curl in PHP. Sorry, I should have seen that.
No problem.
I’m just a little confused at why the alternate connection methods didn’t jump in?
Oh well, at lest you got it working.
Hi great plugin but how do I get it so the date is displayed straight after the separator i.e ‘-’ instead of a new line? basically like the example site you showed. http://medibolism.com/
Hi Eddy,
The date shouldn’t drop onto a new line as the plugin just places them inside a hyperlink tag (a) which is an inline element.
The only thing I can think of is that you might have some CSS that is forcing it onto a new line. Maybe your a tags are set to display as block in your sidebar?
I like the plugin. Feature suggest:
* limit parameter of returning tweets
* parameter to get the tweets in an array, not echo (so you can manipulate the code)
Hi Carsten,
Will try to add the return feature for you, been meaning to do it for a while. Not sure what you mean by ‘limit parameter of returning tweets’ though, could you elaborate on that a little?
I’m having some trouble getting this working. I followed the steps for creating an application, and I entered the consumer key and consumer secret key. I then clicked the “Sign in with Twitter” button, and it just reloads the same page with the following error message at the top: “Could not connect to Twitter. Refresh the page or try again later. Code:401″. I’ve tried several times. Any ideas?
Hi John,
This seems to be a problem that hits certain people & I’ve, unfortunately, never figured out the problem since I’ve been unable to replicate it anywhere to perform debugging on it.
There is only one person I know who fixed it & it turned out they were receiving the error because their host had been banned by Twitter & as soon as they changed their server’s IP address it worked fine.
Just to explain a little more, a 401 error is Twitter’s server telling you are aren’t authorized to access their data. The above is the only reason I’ve come across for that happening.
I’m very sorry for the problem, the only things I can suggest are to try from another server, if possible (just to make sure it’s only occurring on that server), or to use a Javascript based Twitter widget such as SeaOfCloud’s jQuery Twitter plugin. I’ve always managed to get success from it if I’ve had trouble with Twitter & oAuth.
Hello,
Thanks for your response! I’ve done some searching, and there are some people saying that the error can be related to the server’s time being more than five minutes away from Twitter’s time (which is returned in the HTTP headers). Unfortunately, I checked this and my server time matches exactly, so it can’t be that.
I don’t have another server to test with, so I’ve downloaded SeaOfCloud’s plugin and I’ll give it a try. Thanks for the recommendation. Your plugin looked very nice though.
PS: The link you gave is broken; the URL seems to be relative to this blog post / comment page. It doesn’t matter to me, but maybe someone else will try it and get confused.
Sorry I couldn’t help more John, I’m determined to figure out the cause, the main problem is that it hasn’t happened to me, ever, so I’m unable to do any debugging to see if I can find if the problem is due to an incompatibility in the plugin, or if it’s a problem between certain users servers & Twitter.
Sorry about that, missed the http:// off the front of the URL. I’ve fixed it now just in case anyone else gives it a click.
It seems like Twitter changed their oAuth settings because the signup does not contain some of the settings that are describe in the instructions for the plugin. Now there are fields for access tokens under the oAuth tab at dev.twitter.com and there are nowhere to add App Type or Use Twitter For Login.
Maybe I am doing something wrong?
Hi Goran,
Yep Twitter have changed all their oAuth App pages. I haven’t had time to change my instructions, but you should be able to ignore everything that is missing & just enter the callback address.
I have a problem + fix:
if the plugin folder doesn’t have write-rights you can’t write to cache, and it just gives a warning, that there is a problem at writing
Fix:
if($cache === FALSE) { //If cache was set to false we need to update the cache; $fp = fopen($cache_path, 'w'); if(flock($fp, LOCK_EX)) { fwrite($fp, $content); flock($fp, LOCK_UN); } fclose($fp); }sorround that with
Hi Vobo,
That’s actually a great idea. I haven’t had much time to do any upgrades or fixes to Twitter Stream recently with work being so hectic, but I’ll try & put that fix in for you as soon as I can.
Thanks again for your suggestion, and for using Twitter Stream.
Hey there,
I have been using Twitter Stream for a while now and so far it has worked awesome. But now, I can not get the stream to show only one tweet. I even edited the plugins core files to default to 1, but it is still showing 10.
The site is shirtsup.net, and it’s in the bottom of the sidebar.
I had some trouble with this originally using the arguments you supplied with the plugin and never got it to work. Hacking the plug in files did it for me back then, but now it has ignored everything…
Any help would be much appreciated.
Hi Vern,
I’m not sure what could be causing that. I’ve just tried modifying the settings for Twitter Stream here on Return True (It runs my tweets in the sidebar) and it seemed to work correctly.
You say you are using the arguments supplied, did you use it in your template like this:
twitter_stream(array('username' => 'myusername', 'retweets' => TRUE, 'date'=> TRUE, 'profile_link' => 'Follow Me', 'count' => 7, 'show_followers' => TRUE));Obviously you can change the settings to what you want, but is that how you tried it? The only reason I can think it would ignore the settings is that it couldn’t read them for some reason.
If this plugin is used with a multi-site configuration, it only has one cache for all the websites.
So you have to disable the cache if you use different twitter accounts on the different websites.
Is it possible to change this?
Hi Carsten,
I’m not 100% but I think that is caused by the fact Twitter now allows you to omit your username should you be the authorized user. In such a case Twitter Stream uses the name authuser.cache for the cache file.
I can only suggest providing a username as a parameter even if you are the authorized user, in that case Twitter Stream should create a unique cache file for each user.
If you are providing a username & you are still having the problem, please drop as much information as you can via the contact for & I’d love to look into it more for you.