Add Twitter Feed to Websites Without a Plugin

There are all kinds of plugins and code out there for posting a Twitter feed to a website. Most of them ignore that Twitter only allows 150 requests per hour. This often results in Tweets not displaying for websites that get any decent number of hits.

Caching the Twitter Feed in WordPress

The solution is to cache the Twitter feed.

I figured it may be helpful however to post an example of an easy code built on the Nashville music venue, 12th and Porter website, that you can copy and paste and give some brief explanation.

The Code

<pre>

<div id="jstweets"></div>

<?php

include('twitter-cache.php');

$cache = new JG_Cache(dirname(__FILE__). '/cache');

$data = $cache->get('tweets');

if ($data === FALSE)
{
$data = file_get_contents("http://twitter.com/statuses/user_timeline.json?screen_name=12th_and_porter&count=3");
$cache->set('tweets', $data);
}

?>

<script>
jQuery(function(){
var data = &lgt;?php echo $data; ?>;

jQuery.each(data, function(i,item){
ctd = item['created_at'];
cta = item['id'];
ct = item.text;
ct = ct.replace(/http:\/\/\S+/g, '<a href="$&" target="_blank">$&</a>');
ct = ct.replace(/\s(@)(\w+)/g, ' @<a onclick="javascript:pageTracker._trackPageview("/outgoing/twitter.com/");" href="http://twitter.com/$2" target="_blank">$2</a>');
ct = ct.replace(/\s(#)(\w+)/g, ' #<a onclick="javascript:pageTracker._trackPageview("/outgoing/search.twitter.com/search?q=%23");" href="http://search.twitter.com/search?q=%23$2" target="_blank">$2</a>');
jQuery("#jstweets").append(' <div&gtl;<span>'+ ctd + '</span>' + ct + ' <a target="_blank" href="http://twitter.com/12th_and_porter/status/'+cta+'">View &raquo;</a></div> ');
});
});
</script>

</pre>

The Include

This is Jon Giles (JG)’s cache class. I’m not going to explain the caching to you but you can use it as a snippet if you’d like.

Download the code to cache your Twitter feed and put it into WordPress.

Placing Cached Twitter Into WordPress

Place both files in your active theme folder under /wp-content/themes/your-theme/.

Create a directory called ‘cache’. Make sure its permissions are set to 777 so its writeable. Finally do a quick copy and paste over 12th_and_porter for your twitter name.

You can also view other WordPress examples.

approved dosage by doctors fit patients, and 5mg of the elderly and malnourished patients, as well as people with liver failure. The tablet is recommended to be taken http://www.theirishmanwhiskey.com/tour/ right before bedtime.