The Quickest, Dirtiest Twitter Widget Ever

Here’s a simple little way of displaying your latest Tweet, or indeed Tweets, on your blog or website. All you need is the ability to add a bit of PHP into your page – if you’re using WordPress you should be able to do this pretty easily by editing your template.

Step 1: Get your latest Tweets in JSON format

<?php
// Load your most recent Tweets from Twitter, in JSON format
$t=file_get_contents('https://api.twitter.com/1/statuses/user_timeline.json?screen_name=YOUR_USERNAME&trim_user=true');
// Turn the JSON string into a PHP object
$t=json_decode($t);
?>

Step 2: Echo (print) your latest Tweet onto your web-page

<?php echo $t[0]->text; ?>

You could echo more than one Tweet if you wanted to (this API call returns your 20 most recent Tweets) by doing this:

<php
echo $t[0]->text;
echo $t[1]->text;
echo $t[2]->text;
// e.t.c...
echo $t[19]->text;
?>

You could of course use a foreach loop if you wanted to do things properly, but I did say this was a quick and dirty trick! You might want to enclose your Tweet in some HTML so that you can style it and position it nicely with CSS, you can also download Twitter logos and icons from their developer site and the resources page.

The alternative to using this method is using a Twitter widget – these work really well too!