Easily display your last Tweet
I wanted to show my latest tweet on the front page of this site, and although I know there are several plugins which probably could've helped me do this, I decided to see how easy the API was to use.
It turned out to be incredibly easy, as long as you have PHP 5.2 or higher, that is.
This is the code:
require_once(ABSPATH . 'wp-includes/class-snoopy.php'); $snoopy = new Snoopy; $snoopy->fetch("http://twitter.com/statuses/user_timeline/jdevalk.json?count=1"); $twitterdata = json_decode($snoopy->results,true); echo "<p>\"".$twitterdata[0]["text"]."\"</p>";
As you can see, I use the Snoopy library to fetch the data, as that comes with WordPress by default. Than I decode the JSON results by using the json_decode function. This is the reason you need PHP 5.2 or up, as this was only included with this version of PHP.
Next, we output it. Of course, this is a quick & dirty implementation. If I got dugg now, and 1,000 people a minute came looking at that page, it should have some sort of caching in there. For now though, this is fine as it is!
Update: As Kim noticed in the comments, I've added some code to automatically link any @username mentions to those usernames. Replace the last echo line above with this:
$pattern = '/\@([a-zA-Z]+)/'; $replace = '<a href="http://twitter.com/'.strtolower('\1').'">@\1</a>'; $output = preg_replace($pattern,$replace,$twitterdata[0]["text"]); echo "<p>\"".$output."\"</p>";
You could even decide to add rel="nofollow" the $replace if you don't want those links to give juice.
Update 2: To make it even more complex, here is the entire code I now use, which excludes replies and caches the Twitter API requests so you won't overload the API:
require_once(ABSPATH . 'wp-includes/class-snoopy.php'); $tweet = get_option("lasttweet"); $url = "http://twitter.com/statuses/user_timeline/jdevalk.json?count=20"; if ($tweet['lastcheck'] < ( mktime() - 60 ) ) { $snoopy = new Snoopy; $result = $snoopy->fetch($url); if ($result) { $twitterdata = json_decode($snoopy->results,true); $i = 0; while ($twitterdata[$i]['in_reply_to_user_id'] != '') { $i++; } $pattern = '/\@([a-zA-Z]+)/'; $replace = '<a href="http://twitter.com/'.strtolower('\1').'">@\1</a>'; $output = preg_replace($pattern,$replace,$twitterdata[$i]["text"]); $tweet['lastcheck'] = mktime(); $tweet['data'] = $output; $tweet['rawdata'] = $twitterdata; $tweet['followers'] = $twitterdata[0]['user']['followers_count']; update_option('lasttweet',$tweet); } else { echo "Twitter API not responding."; } } else { $output = $tweet['data']; } echo "<p>\"".$output."\"</p>";
As you can see it also saves the amount of followers you have into $tweet['followers'], which I then use to display this:
<p>
<a href="http://twitter.com/jdevalk">
<?=$tweet['followers']?> followers on Twitter, and you?
</a>
</p>Update 3: Another fix to automatically make all links clickable, below this line:
$output = preg_replace($pattern,$replace,$twitterdata[$i]["text"]);
Add:
$output = make_clickable($output);
This will use the WordPress internal make_clickable function to make sure that all URL's are clickable.
Enjoy!
Related posts
- Showing off your FeedBurner subscribers
- Using the Technorati API on your blog
- Treat those first time commenters with respect!
- The power of Twitter (clients) exposed
- Install WordPress 2.6.3







Wow awesome share here Joost, was just looking for something like that about 3 hours ago, thank you :)
Thanks! This is great. I've been using twitter tools to do the same thing but would rather not depend on a plugin. Would it be complicated to have it remove any @ replies?
@Kim nope, I'll be working on that a bit.
I'm a total dunce when it comes to these things and generally need exact step-by-step instructions. When I tried this on my own site the @name was not linked nor was the link that I was sharing. I see on your front page that username of the person that you were talking to was linked. Am I missing something completely obvious?
Also, I have never commented here until today but I want to thank you. I learn so much from your articles and your newsletter.
Glad to be of service Kim, and no you didn't miss anything, I added some code later on to do that ;) I've updated the post to add it :)
Just updated the post with code that excludes replies and caches the requests, so you won't overload the Twitter API.
Thanks so much!! You're the best :-)
Ok - you're going to think I'm the biggest pest. I can use the original example and change it to link to the username but when I use the full code I get the following error
Missing argument 1 for Snoopy::fetch(), called in /home/kimwood/public_html/stage/wp-content/themes/kimwoodbridge/header.php on line 73 and defined in /home/kimwood/public_html/stage/wp-includes/class-snoopy.php on line 123
Thanks!
Hey Kim,
Joost just missed the variable out the fetch. Change line 6 to:
$result = $snoopy->fetch($url);
and that works great for me. Cheers for the code Joost!
Thnx for the code. Something to play with.
Regards
Champi
Thanks Anthony!
Got it - it's in the header of my website, if you want to check it out. :-)
Thanks Anthony, updated the post!
Just installed WP-Syntax too to make the code a bit more readable, but I don't know yet whether that's a keeper or not :)
And another small update to the post to reflect another change I made in my own code :)