Themergency fooplugins.com

Using the Twitter API in WordPress

| 0 comments

Since Twitter changed their API in June , you can no longer easily pull tweets from someone’s timeline. It can still be done, but you now have to use OAuth to use the API calls. This means you have to write server side code to authenticate with Twitter and then pull the data you need. Unfortunately, this has had a negative impact on javascript libraries pulling tweets from a timeline. In other words:

You can no longer pull tweets from the API by only using javascript!

That sucks. And it means my FooBar jQuery plugin no longer supports pulling tweets into the messages. Fortunately, this is not the case for the WordPress version, but this meant I had write some code to pull tweets using OAuth.

Twitter OAuth

If you have no idea what OAuth is then basically, it allows you to securely authenticate yourself with Twitter without having to pass your username and password over the wire. Read more about the OAuth protocol.

And Lucky for me, someone else had already written a PHP wrapper to handle OAuth with Twitter : the twitteroauth PHP library.

Hello FooTweetFetcher

So even though most of the hard work had been done for me, I wanted it to be even easier. I am lazy like that. Here were my requirements:

  1. I did not want to hit the API on every page load.
  2. I wanted to cache the result from the API and store that for X minutes, using WordPress transients.
  3. I wanted to get an array of tweets with 2 lines of code.
  4. I want to convert the resulting tweet text into clickable HTML.

So I wrote a reusable class called FooTweetFetcher, and it allows you to do all of the above with minimal effort. I wrote it because I knew I would be needing this in future plugins, so now I just need to include the class and I’m all set to get tweets. Fork the code on GitHub:

FooTweetFetcher

Store Your Twitter Application Details

In order to use the class, you need to pass it your Twitter application consumer and access keys and secrets. These four fields can be found on your Twitter application details page:

twitter application

And you need to store these details somewhere in your plugin or theme so that you can use them. In FooBar, I store them on a new tab on the settings page:

FooBar Twitter Settings

Cache Tweets Using Transients

If you hit the Twitter API on every page load, your page load time will be slow! Just don’t do it. And besides, who will be tweeting every few seconds in any case. So once I pull the tweets, I save the result in a transient for later use. I cache the tweets for 5 hours, but you can easily change this timeout.

Simplest Usage

//include required dependancies
require_once 'twitteroauth.php';
require_once 'FooTweetFetcher.php';
//Register a new appplication at http://dev.twitter.com/apps/new
//Below are fake twitter app details
$twitter_consumer_key = 'YUsyMo5NicXhE4v1WrxdPb';
$twitter_consumer_secret = 'GTFbRUwmzaQ81xLvhYkXfONCyEdSsIZoVDrqMn5g';
$twitter_access_key = '393392201-UnWGXxQYOwJsdaycrbIztpVlEq2kZMHvR64hKPoi';
$twitter_access_secret = 'PNquKXvpgYGcDRilrwFZtSHsUWjCOTeAJf28EM0oQ';
//create a new instance
$fetcher = new FooTweetFetcher($twitter_consumer_key, $twitter_consumer_secret, $twitter_access_key, $twitter_access_secret);
//get me some tweets
$tweets = $fetcher->get_tweets( $meta["twitterUser"], $args );
foreach ( $tweets as $tweet ) {
	//convert all URLs, mentions, hashtags, media to clickable links
	$text = $fetcher->make_clickable( $tweet );
	echo $text;
}

Clickable Tweets

You will see I also call a make_clickable function on the FooTweetFetcher class. This takes a tweet object as the parameter and returns an HTML tweet with all mentions, urls and hashtags converted into anchor tags. Very useful stuff.

Fork, Use and Improve

Please download and use the code within your plugins and themes. If you can improve it, please submit a pull request. If you use it in a project, submit a pull request and I will add it to the list.

0 comments
  Livefyre
  • Get Livefyre
  • FAQ