WordPress Plugin: Twitter Stream

/ WordPress Plugins / by Paul Robinson / 445 Comments
This post was published back on December 30, 2009 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

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 – 04/03/2013

If you have updated the plugin to version 2.5 and are having trouble, please make sure to delete the cache. Either manually or using the button on the options page.

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?

  1. Connect to Twitter securely using oAuth. (required)
  2. Show the twitter timeline for any public username.
  3. Choose how many tweets to show.
  4. A Widget or template function is available.
  5. File caching to stop API overuse.
  6. Optional date shown in xx ago format, also links to permalink for the tweet. (Requested by Ron)
  7. Customizeable via CSS. (see the ‘Can I Style It?’ section below)
  8. Authentication for better API limiting & ability for protected users to show their tweets.
  9. @replies now link to the user profile of the user you are replying to.
  10. #tags now link to the Twitter search page for that hash tag.
  11. Link to user’s profile, customizable via CSS & via function parameter.
  12. Follower count can now be shown.
  13. 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:

  1. Download the zip file and unzip it to somewhere, say your desktop.
  2. With any good SFTP client upload the extracted folder to your wp-content/plugins/ folder.
  3. 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:

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:

  1. .at-reply for @replys.
  2. .hash-tag for #tags.
  3. a.twitter-link for autolinked URL’s within the timeline.
  4. a:hover.twitter-link for autolinked URL’s within the timeline when they are hovered over.
  5. a.twitter-date for the date permalink.
  6. a:hover.twitter-date for the date permalink when it’s hovered over.
  7. .profile-link for the newly added link to user profile.
  8. .follower-count for the newly added follower count.

Can I Have A Options List?


Sure. The basic usage for in the template is this.

Here is the info for each parameter.

  • username should 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.
  • count should be a string and is how many tweets to return. Default ’10’.
  • date bool 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_link a 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.
  • retweets bool, true or false. Shows retweets with normal tweets.
  • show_followers bool, true or false. Shows the number of followers.
  • cache_time Set the length of time tweets are cached for.
  • echo Controls 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.

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.

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.

445 Comments

Author’s gravatar

Sadly, I have tried all the work arounds and nothing seems to be working. I am still getting the message below.

Authorizing Twitter Stream with Twitter is no longer optional. You need to go to the Twitter Stream Authorization page in the WordPress Admin (under settings) before your tweets can be shown.

Reply
Author’s gravatar author

Hi Phiri,

That message is shown when you haven’t yet authorized Twitter Stream to access your Twitter account.

The error is returned by oAuth Twitter’s authorization system. Have you tried going through the authorization process by visiting the settings page?

Author’s gravatar

Hi Paul,

I am using your plugin on a website, and everything has worked well so far, until now. Suddenly, only 1 tweet (instead of 3) is showing. Do you have any idea what the cause could be?

Thanks!

Reply
Author’s gravatar

BTW: I am using the function call.

And, maybe I found the solution to my problem by myself: could it be, that if you set retweets to FALSE, the RT-tweets are not displayed, but are still considered in the count? In my case the count is set to 3. The situation was that 2 of the last 3 tweets in the twitter account were RT. And the plugin only displayed the one that wasn’t a retweet.

Was that too complicated? Or did you understand the problem?

Author’s gravatar author

Hi Joerg,

That is indeed the problem.

Unfortunately the counting & supplying of Tweets is all done by Twitter themselves and then handed back to the plugin. It is something that has always bugged me about the Twitter API but I am unable to fix.

Author’s gravatar

Hi
I have just downloaded this plugin as it was excatly what I needed, and I am setting it up now. I have looked at the files and for some reason cannot find where you control the “output”. I would be interested in removing the link from the dates after my tweets. thank you.

Reply
Author’s gravatar author

Hi Pumi,

You would want line 383 in twitter-stream.php it looks like this:

If you remove the hyperlink it would be:

Just remember that whenever there is an update you will have to redo it as it will overwrite it.

Author’s gravatar

Wow, thank you for the superfast reply!

Author’s gravatar

Hi, I seem to be getting the following error whilst trying to connect via the Sign In With Twitter button in the admin settings.

‘Could not connect to Twitter. Refresh the page or try again later. Code:0’

I’ve had this error now for about 3 days.

Any ideas what the problem is?

Reply
Author’s gravatar author

Hi Nick,

I’ve just reset my keys here at Return True just to make sure it wasn’t a problem with the plugin & it doesn’t seem to be.

The ‘Code: 0’ error is thrown by Twitter’s servers and as far as I know it is a strange error that normally means there is some sort of connection/communication error, but I’ve never heard of it going on for more than an hour never mind 3 days.

Normally one other cause is that when you created the app during the authorization process you didn’t select ‘Web App’ but twitter no longer require you select that any more. The only thing I would suggest is just to make sure the callback is filled in on your ‘My Applications’ page on the twitter website. It can be found at https://dev.twitter.com/apps

Author’s gravatar

Hi Paul,

Ok I tried it again with my own WordPress installation on a MT server, and with my own Twitter account – and it worked fine.

Tried it again from scratch with my clients Server and Twitter account – and it failed.

Any ideas what may be causing that? Her Twitter account?

Reply
Author’s gravatar author

Hi Nick,

You could try opening the twitteroauth.php file inside the twitteroauth folder (inside the plugin) and adding var_dump($response); after line 226. Then try to complete the authorization again. This time it should fail but with an message. If you can post that message I might be able to help out further.

Also check one other known problem is if the servers clock is not set using NTP Twitter will throw an error.

Author’s gravatar

string(146) “oauth_token=tg2GdT9rQAo26wLJDQiyFNjIRQdjX8PUNLo5pQeZx1k&oauth_token_secret=****&oauth_callback_confirmed=true”
Warning: Cannot modify header information – headers already sent by (output started at /****/****/****/wp-content/plugins/twitter-stream/twitteroauth/twitteroauth.php:227) in /****/****/****/wp-content/plugins/twitter-stream/redirect.php on line 21

Reply
Author’s gravatar author

I honestly have no idea. I’ve removed a few things from your comment as they shouldn’t be left open to the public, especially your oAuth secret token.

Everything that you’ve pasted is exactly the same as what I get when I reactivate my version of Twitter Stream. It’s very odd that it’s only on that server. I can only think there is something on the server somewhere causing a compatibility problem.

I would offer to help debug, but I would need server access as it would be near impossible to debug without it. Drop me an email if you’d like me to try and help out further.

Author’s gravatar

Hello! Thank you for the Twitter Stream plugin! Really Great!

I have a question: How can i add the Twitter follow button in “Visit My Profile” place?

Thank you!

Reply
Author’s gravatar author

If you are using the sidebar widget you may be able to paste the code into the field for customizing the ‘Visit My Profile’ text.

If you are using the PHP function you could try passing the code via an array. So something like:

You would have to remember to escape (backslash) any single quotes though.

Author’s gravatar

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…

Reply
Author’s gravatar author

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

Author’s gravatar

Ok i see! Thank you for your help! I thing this will be nice feature for the next update. 🙂

Reply
Author’s gravatar author

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.

Author’s gravatar

In the section after authorization there’s a ? missing ->
What Do I Do Now?

Reply
Author’s gravatar

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?

Reply
Author’s gravatar author

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.

Author’s gravatar

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

Reply
Author’s gravatar

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!

Reply
Author’s gravatar author

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.

Author’s gravatar

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

Reply
Author’s gravatar author

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.

Author’s gravatar

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 🙁

Reply
Author’s gravatar author

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.

Author’s gravatar

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

Reply
Author’s gravatar author

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.

Author’s gravatar

Hi Paul,
Thank you for responding and helping. I’ll let you the outcome.

Reply
Author’s gravatar

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

Reply
Author’s gravatar author

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.

Author’s gravatar

Thanks a lot! That was an easy fix, just enable curl in PHP. Sorry, I should have seen that.

Author’s gravatar author

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

Author’s gravatar

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/

Reply
Author’s gravatar author

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?

Author’s gravatar

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)

Reply
Author’s gravatar author

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?

Author’s gravatar

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?

Reply
Author’s gravatar author

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.

Author’s gravatar

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.

Author’s gravatar author

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

Author’s gravatar

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?

Reply
Author’s gravatar author

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.

Author’s gravatar

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:

sorround that with

Reply
Author’s gravatar author

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

Author’s gravatar

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.

Reply
Author’s gravatar author

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:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more