WordPress PHP proxy/helper pages for Twitter API

Ok I am currently doing some work on my personal blog.

On the home page I am displaying my latest tweet using a bit of jQuery code.

$.getJSON('https://api.twitter.com/1/statuses/user_timeline/killfall.json?count=1&callback=?', function(data) {
    tweet = data[0]['text'];
    tweet = tweet.replace(/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i, "<a target=\"_blank\" href=\"$1\">$1</A>");
    $('#tweet-text').html("&ldquo;" + tweet + "&ldquo;");
});

This just gets my latest tweet from twitter, converts any URLs to anchor tags and puts that in a div with an id of tweet-text.

However I feel I should probably be using oauth and the newer version of the twitter api due to the fact that I keep getting an error message saying

"error":"Rate limit exceeded. Clients may not make more than 150 requests per hour." 

and I believe using oath will solve this problem.

The thing I want to know is how it would be best to implement this. I want to continue to use ajax in jQuery to load the tweet so that it loads it after the page has finished loading.

My instinct makes me think I should have some JavaScript like

var jqxhr = $.ajax( "<?php bloginfo('template_directory');?>/ajax/get_tweet.php" )
.done(function() { 
    tweet = tweet.replace(/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i, "<a target=\"_blank\" href=\"$1\">$1</A>");
    $('#tweet-text').html("&ldquo;" + tweet + "&ldquo;");
 })

And then create a php file which would act as a proxy/helper in the theme folder in a dir called ajax called get_tweet.php which would do the oauth stuff and just echo the latest tweet which would be used by the JavaScript.

But I’m just wondering if putting random php files inside a wordpress theme like this is good practice or if there is a better way of doing it?

Leave a Comment