oAuth Twitter Feed: PHP Library & WordPress plugin

Twitter’s new API 1.1 has been live for a couple of months now and that brings with it a whole new set of requirements for using the Twitter API. From March 2013, everyone displaying tweets must comply with the new terms and update their websites code to be compliant with the new requirements, which includes changes to the way you authenticate.

This basically means you won’t be able to use Javascript to load in tweets for your website directly from Twitter, as all requests must be authenticated.

This is a major problem for the vast majority of websites that are currently using Twitter and one we needed to work around – so we built a PHP class (GitHub), and a WordPress plugin (GitHub), that implements all the new requirements for authentication and gives you an array of tweets out of the other end, for you to use in your WordPress themes, or PHP applications. We built the class and plugin on top of Abraham Williams’s Twitter OAuth class which makes the OAuth stuff really easy.

You can get the plugin from the WordPress plugin directory: oAuth Twitter Feed for Developers.

The plugin implements caching for (by default) 1 hour too, to stop you hitting the API limits which could occur with the new authenticated-only request requirements and protects you from Twitter outages. You can change this yourself in the source code.

It’s definitely for developers though – you only get an array out of it that contains Twitter tweet objects (You’ll find more about them on the Twitter API documentation). You’ll still need to style the output and make it comply with the new display requirements.

The plugin provides a getTweets() function that you can call in your theme files. This returns an array you can then loop over and do whatever you want with it.

      $tweets = getTweets();
      var_dump($tweets);

      foreach($tweets as $tweet){
        var_dump($tweet);
      }

You can specify a number of tweets to return (up to 20) by passing a parameter to the function. For example, to display just the latest tweet you’d request getTweets(1).

This is an early release of the class and plugin, both could offer a lot more functionality.  If you want to get involved, fork the code on GitHub and send us a Pull Request!

By Liam Gladdy

Liam provides the crucial the link between the development and design teams here at Storm, effortlessly connecting the two parts so that all of our sites and web applications look great and work seamlessly.

He fosters a healthy love of technology and video games as all geeks do, but uniquely he occupies the oft perilous world of being both an Apple and Microsoft fanboy - he'll happily fulfil his role as an Xbox Community Ambassador from the comforts of his MacBook Pro. Not to mention his gamerscore, which is larger than the rest of ours put together.

Liam claims to be unbeatable at the Lady Gaga level on Dance Central, and not one of us has yet been man enough to challenge him on this statement. One day Liam, one day.

gladdy.co.uk →

3 comments

  1. Thanks for the great info and links to the libraries. I’ve downloaded the libraries (not the wordpress plugin)and used the basic example for how to implement it. I filled in my tokens etc however I get the following output only: #0 StormTwitter->cropTweets(, 1) called at [StormTwitter.class.php:65] #1 StormTwitter->getTweets(1, music) called at [/nas/students/a/a7-lucas/unix/public_html/BMS/angus.php:21]
    Warning: array_slice() [function.array-slice]: The first argument should be an array in StormTwitter.class.php on line 72

    NULL

    Any idea on how this could be fixed?
    Thank you

  2. Hi! thanks for this great plugin.
    Do you think I can use your plugin to expand it for mor thing like get friendships lookup and other twitter API calls?