Twittero: An ExpressionEngine Twitter Plugin from Strawberry
Posted on Tags: Web, Social Media, ExpressionEngineWe can already hear you thinking “not another Twitter plugin?!” Don’t worry – this is one with a difference.
A few weeks ago the Strawberry team ran into a problem when requesting Tweets on some of our websites that pull in their respective Twitter feeds. The problem was that unauthenticated calls are permitted at just 150 requests per hour; unauthenticated calls are measured against the IP of the server or device making the request. We have several servers but inevitably each server hosts several sites which leaves us with some sites having the same public facing IP. With the ever growing amount of our sites that involve Twitter feeds, we were starting to reach the 150 requests per hour limit fairly often.
So what was the solution? The fundamental solution behind it all was to use OAuth; Twitter's API authentication method. This would allow 350 requests, per hour, per authorised feed. Basically, it means that once authorised, each site could make 350 requests per hour. You can read more about rate limiting in the Twitter documentation.
My first thought to solve the problem was to find an ExpressionEngine plugin that uses or allows OAuth authentication. Unfortunately, there isn't one. I then thought, could I take the existing first-party Twitter Timeline plugin from the guys at ExpressionEngine? There laid another obstacle - after looking through it, it lacked features we needed and I felt that I could do a better job myself.
Features Missing from Twitter Timeline
There were several things that the Twitter Timeline couldn't do ‘out of the box’ (including working all of the time):
• Use the OAuth method to pull in tweets.
• Support custom cache expiry time (if you don't get much traffic, you could even disable the cache).
• I found that the Twitter Timeline sometimes didn't link certain parts of a Tweet. Sometimes link URLs, usernames and hash-tags were not linked.
• Allow the user to flip between time/date styles (I wanted the ability to flip between date wording like 'About 3 days ago' and regular date formatting).
• Offer the ability to disable retweets.
Required Features
After discussing requirements with the team, and through several iterations of the Twittero plugin, the final feature list includes:
• Ability to exclude retweets.
• Show replies.
• Custom cache expiry time (and have ability to disable cache during development).
• Custom time formatting as mentioned above.
• Number of tweets to pull in via the API, and number of tweets to show (due to the structure of the Twitter API, and to prevent multiple API requests, this may need to be different depending on the showing replies option as this cannot be exlcluded via the API).
• Custom HTML formatting using standard EE-like template tags, i.e. <li>{twittero_tweet} {twittero_time}</li>.
The Build
Credit where credit is due, the initial base of this project uses @abraham's ‘PHP twitteroauth Library’, as recommended by Twitter. This library allowed me to dive straight in and build the plugin. Previously I had built a few small ExpressionEngine plugins and have had years of experience tinkering with PHP, so I was already familiar with the setup of it all.
One of the main things I wanted to get right was the caching of the Tweets. A really popular website could easily eat through 350 requests per hour. I took the approach of creating a simple text file and storing the Tweets in there. The plugin then compared the cache file timestamp to the cache limit set by the ExpressionEngine tags within the templates.
The plugin has turned out a lot better than I had first planned, steadily getting better and better. Now it is at a stage where we at Strawberry feel it can benefit other ExpressionEngine developers. Don't worry, it's free.
You can download Twittero and keep up to date with its development on GitHub. Let us know what you think – your feedback is always appreciated!
Note: This plugin can only be used once per page.