While working on the redesign of my website I realized I was using a very inefficient method of displaying my latest Twitter post (“Tweet”). Currently I’m using a script I found online that uses two functions that are just over 20 lines of code total. Here I’ll show you how to use WordPress’ built in RSS parser to display your latest tweet in 5 lines of code (thus this will only work on WordPress).

Note: An important aside about this script is that by default WordPress caches the feed for 1 hour. This means your Tweet on your website will be updated at that frequency. This is great because it saves your server a lot of overhead, especially on busy website. However, if you’re an avid Twitterer and want your very latest Tweet shown for every page refresh for every visitor, I recommend using this method.


function wp_echoTwitter($username){
     include_once(ABSPATH.WPINC.'/rss.php');
     $tweet = fetch_rss("http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1");
     echo $tweet->items[0]['atom_content'];
}

Past the above in your functions.php file. Now just paste the following in your theme file where you want your Twitter post to appear:


<?php wp_echoTwitter('johnkolbert'); ?>

Obviously replace my username with yours. Now you’re done! Style it however you like with HTML and CSS.

Update:

If you’d like to show more then one tweet, here is an updated version of this function: http://jhnk.pastebin.com/5A4aXYTW


Reply

Craig

August 31, 20109:33 am

Do I need to change my twitter settings? Your username successfully pulls in your last tweet but when I try mine I get nothing.

Reply

John Kolbert

August 31, 20108:12 pm

You shouldn’t need to. If you haven’t tweeted for a while (like days or weeks), it occasionally shows nothing. But otherwise it should just pull up your latest tweet.

Comment on this post:

License: By commenting you are granting me a perpetual, non-exclusive license to reproduce, paraphrase, and display your words, name, and/or website on this domain. All comments subject to moderation, editing, or deletion at my discretion.

Important: Unfortunately, I'm unable to offer support via comments. However, feel free to hire me.

Entering code?

(optional)