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


Craig
August 31, 20109:33 amDo I need to change my twitter settings? Your username successfully pulls in your last tweet but when I try mine I get nothing.
John Kolbert
August 31, 20108:12 pmYou 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.
Dan
September 6, 20104:36 pmHi John,
nice and simple script!
The script is working fine for me, but the feed is not cached!
I’m running WP 3.0.1.
Darfuria
September 10, 20103:13 pmAnyone know how to grab the date of the tweet as well?
sub-zero
September 12, 20104:40 pmThanks I’ve been looking for a way to do this!
Richard
September 13, 20101:56 amHi John, thanks for the post. Does this code need to be updated in light of the recent change Twitter made to their API? I don’t know exactly what the change was but I do know that I had to re-add all my accounts to Tweetie. Do you know anything about that? Thanks.
adam
September 21, 20108:43 pmYou’d want to change where it says “atom_content” to “updated” so it would look like this (sorry if the code doesn’t come out great in this comment)
function wp_echoTwitterDate($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]['updated'];
}
That’s if you wanted to make like a second function to use along with it. You could save your server some time by just storing the two variables into a string and echoing the whole string instead of two functions but it’s whatever works.. explore! but that should help you get the date. Keep in mind it’s going to come out in some odd format so you may want to change it using something like this:
echo date(‘m/d/Y’,strtotime($tweet->items[0]['updated']));
instead of just echoing the raw data. Basically the strtotime() function changes the raw data into a timestamp and the date() function formats it how you’d like. If you want more on working with the date() function do a google search(php date). hope this helps!
Ash Robbins
September 28, 20102:53 pmJohn Kolbert I love you!
I’ve been trying to work this out for a couple of hours and kept getting bulletted list items, but your solution works perfectly! Thanks
Ash
Mark Garity
October 8, 20101:40 pmStrange. I get the error “rss.php is deprecated since version 3.0! Please use class-simplepie.php”
BrianK
November 24, 20107:16 amI know you wrote this a while back, but want to let you know it’s awesome.
KevinD
December 29, 20105:54 pmI’m trying this out but it seems to have an issue with tweets that contain a link. Any ideas on what I might be doing wrong?
Sean
January 5, 201112:22 amThanks for this, mate. Just what I was looking for. Does this support links @tags and #tags? If not how would I go about implementing it?
Moz
January 29, 20114:01 pmWarning: I tried this code and it seems to have damaged the entire page. Do not use this code.
John Kolbert
January 29, 20115:21 pmYou must be using it incorrectly. I use this on many sites and just tried it on the latest nightly build of WordPress 3.1 and it’s working just fine.
Mike Key
February 1, 20112:01 pmYup, it works just fine on WordPress 3.1
Great little simple snippet.
Jim
February 2, 201111:35 pmAbsolutely a great solution! Thanks for this.
Only one thing, I’m looking for a way to reduce the number of characters of the outputted tweet and add … at the end of the tweet (so, three dots). How to do this?
Thanks
Jim
noush
June 10, 20119:57 pmhave you figured it out how to display specified number of words from the latest tweet with clickable arrow [>>] or dots [... ]to read furture?
Jay Pick
February 5, 201111:18 pmSpot on! Cleanest solution I’ve found. Works a treat, thanks
Alasdair
February 6, 20114:12 pmHow would you get the date from the atom feed, with the “about a minute ago” etc. formats? Does Twitter send those out or would we have to do some coding to achieve it?
Great code though, thanks.
Alasdair
February 6, 20114:13 pmNote to self: Read all comments next time! Ignore me!
Jim
February 11, 201111:30 pmI’m using this version. I replaced the rss.php because it is depreciated and I added 3 dots at the end in case I have to shorten the tweet.
One thing I noticed with this script that when there is a @ in the tweet, the link stops at that point. This is the tweet I am talking about. http://twitter.com/SonyOnline/status/35522732640706560
This is the code:
http://jhnk.pastebin.com/EyN0Axpr
Bk Designs
February 15, 20119:26 amThanks for this I havent been able to pull twitter onto my website design and this does it professionaly and perfect!
Drew
February 25, 20115:36 pmSimple solution – thanks
Daniel Hellier
March 10, 20115:01 amHey, I edited your code a little and well, yeah.. http://danielhellier.com/latest-tweets-function/
Elijah Paul
March 25, 20114:09 amHey Daniel,
I edited your code a little further to display the time past since the tweet(s) rather than
the date format. http://bit.ly/fkLWNB
Lena Shore
June 18, 20115:37 pm[ X ] You Receive A Virtual Hug.
PERFECT! And no stupid plugin to make it happen! Fabulous!!! Thank you!!!! Works like a dream!
Jimmyjames
June 22, 20119:54 pmTHANK YOU!!!! I was getting so sick of Jetpack’s Twitter widget messing up.
Adem
July 21, 20111:37 pmWorks great! Much better than trying to get the masses of Twitter widgets working.
Sagive
August 13, 201110:41 pmThanks a lot man.. simple and easy to inegrate with my options page (building a template).
Cheers, Sagive
hankd
September 8, 20112:49 amThis code totally rocks. But I’m having an issue with it. I get everything to work fine with username A, but when I put in username B I get nothing. I switch it back to username A and everything works fine again. Does anyone know if there is a setting in twitter that needs to be set or unset in order to work? Thanks again for the sweet code.
Brian Krogsgard
September 27, 20116:03 pmI had the same issue. It’s built using search, and if you don’t tweet for a couple weeks, Twitter drops it and you get nothing. Either tweet enough to support this method, or find another solution.
I just adapted it to the other one posted here: http://danielhellier.com/latest-tweets-function/
Mark Schoones
October 12, 20119:57 amHow do i style the tweet with CSS? How do i link the php-script with the css?
Josh Stauffer
October 14, 20112:16 pmOnly problem for me is that the Search API only indexes the last 6-9 days of tweets. If a user doesn’t update often, zero results could be returned.
Other than that, this would work like a charm and I appreciate the efficient code.
johnny
December 4, 201112:31 amsimple. lean. effective.
thanks!
Jason King
December 8, 20112:56 pmThanks, seems to be working fine.