Tutorial: Display your Latest Tweet on your Website
Posted on Saturday, June 13th, 2009 under Tutorials, Web Design
UPDATE – I have posted a more recent ‘part two’ post which uses SimplePie to retrieve your Tweets. Check out the post over at http://tr.im/yxMF
You will have noticed that I display my latest Tweet in the header of my website (look up). Until recently I have been using the Twitter for WordPress plug-in, but I was experiencing problems and 9/10 times my page loaded I would get the error message instead on my Tweet! This became increasingly frustrating, as my site would get featured on someone else’s blog and the screenshot would contain this error message. After persevering with this plugin and contacting the author, I came to the conclusion I need to look elsewere.
Anyway, less of the chatter Stu! Here’s the code….
$username = "TwitterUsername"; // Your twitter username.
$prefix = ""; // Prefix - some text you want displayed before your latest tweet.
$suffix = ""; // Suffix - some text you want display after your latest tweet.
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed($feed) {
$stepOne = explode("<content type=\"html\">", $feed);
$stepTwo = explode("</content>", $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace("<", "<", $tweet);
$tweet = str_replace(">", ">", $tweet);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>
Make sure you change the ‘TwitterUsername‘ to your own Twitter Username. If you want to keep your code clean, then you can move this script into your header tags, but you will need to change this line:
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
to this:
$tweet = stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
Then you can just call this variable later on in your code where you want your Tweet to be displayed. Finally, I just want to mention that if you come across the following error:
Warning: file_get_contents(): URL file-access is disabled
You will need to do one of the following:
- Edit your PHP.ini file (usually located in /etc/php.ini) and make sure this line is in it:
allow_url_fopen = On - Create a file called “.htaccess” in your website root directory with the following line:
php_value allow_url_fopen on
This should sort out the problem, but if you need any more help then leave a comment and I will gladly help! If the whole process went smoothly, then you should be appreciating your Latest Tweet on your website just like me!

Is it possible to show the last 5 tweets instead of only the last one?
@Dotidentity – Sure mate, all you have to do is change the value of rrp to however many tweets you want to show, for example change the following line taken from the code…
$feed = “http://search.twitter.com/search.atom?q=from:” . $username . “&rpp=1″;
to..
$feed = “http://search.twitter.com/search.atom?q=from:” . $username . “&rpp=5″;
and this should give you your 5 most recent tweets, hope this helps :)
Hi,
Is it possible to limit the character count? Since when I post bigger tweets it will blow the whole div up. which isn’t what I want.
Marcel
I used another Drupal snippet in the meantime.. but thanks!
Changing the “&rpp=5″ won’t work for me, for some reason? Also, is there any way of displaying time and date along with the posts?
Cheers mate :)
@Dotidentity @mattcouchman – sorry guys I have been away for two weeks so had no internet access! I tested the rrp=5 on a blank php and it worked for me, very strange, let me check again and get back to you also abou tthe time and date too you asked for matt
Hi Stu,
Great little script, I’m using it to display various updates about my website, life etc.
I’m using the WordPress loop to display the last 3 posts, for some reason your script and the WP loop don’t get on. It took me a while to work out what was going on. If you want to work with WP on the same page. You need to add the line $feed = “”; before doing any WordPress stuff.
Anyway thanks for making script available!
Elliott
Hi Stu,
I’ve been looking into a way of showing tweets on websites, and yours is the cleanest I’ve found. Thanks!
One question though, have you figured out a way to show how long ago the tweet was posted?
Thanks!
Sarah
Very nice m8! Just got one question for ya, how do I format the text? Cos CSS doesnt seem to work :S
Jos put the whole “tweet” inside a DIV and give it an id of “mytweets” (eg: in your CSS file you can then change anything you like be using the id first eg:
#mytweets a
{
color: red
}
will turn any links into red…
Make sense?
Last post went all weird.. shout if you need me help, either here or on my site
Hello,, I am having the same difficulty as others — only one tweet showing, the last one, even though I changed “&rpp=1″ to “&rpp=4″.
Any recourse here? Thanks!
Hi Guys!
I am in the process of writing Part 2 which will let you customize your twitter feed in lots more ways and allow you to bring in as many tweets as you need to so keep checking back or follow me on twitter or subscribe to my RSS feed at the top :) it should be up in a couple of days!
Cheers
Stu
The post is now live, using SimplePie is a much better option – http://tr.im/yxMF
hello, this is very nice. i’ve been looking for a script like this. anyway, i have a little proble, the tweet went out just fine but two lines of error appeared under my tweet.
here is the error:
Warning: Missing argument 1 for parse_feed(), called in C:\Inetpub\vhosts\rodsyconstruction.com\httpdocs\able\twi.php on line 29 and defined in C:\Inetpub\vhosts\rodsyconstruction.com\httpdocs\able\twi.php on line 7
i just saved a php file inside the \able folder with your code in it. I don’t know if i have to edit anything else. And I can’t see any etc folder inside my web host folder. I hope you can help. thank you very much for this!
Oh, its ok now, im sorry i didn’t see you included the print code (echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);). mine is only (parse_feed($);) that is why it had errors. but its ok now and thank you so much. :)
Hey, I’m sorry if I’m causing a spam, I just want to ask again, how about on blogger?
i tried changing the url of the tweets and put my feed burner feeds from blogger and it actually worked. But the only thing it displayed was the title of the very first blog.
Can you teach us how to include the first 5 entries with the (some)description in it?
thank you so much.
@mark – sorry mate just checked the post and the code viewer had cut of half the block of code, can you try copy it again into your webpage for me? also I have released another post which shows you how to use SimplePie to pull your tweets, this is a much better method and lets you customize more! Check it out here
Hey Stu, that was my bad. its working now cuz I haven’t included $twitterFeed in echo parse_feed($twitterFeed).
anyway, do you also have solutions for blogger? i also want to display the first five of my post on my website. If you can also help me with that. thank you so much!
@mark – I haven’t really used blogger at all tbh mate, but check out my newer post, using SimplePie you can set how many tweets, you can also include the date, hyperlink of the tweet, that kind of thing, hope it helps – Click here to go to the newer post.
No problemo Stu, everything works out great. I just read your post and actually implemented it already. hehe. thank you very much!
Hi, I used your code on http://www.brahimusic.com
But my question is how can I display a ‘ instead of it saying ‘
Ah and how can I display the timing of the tweet?
Hi,
I used this code on http://www.brahimusic.com, but have a few questions
How can I display a ‘ now it says ‘?
And in Firefox there is a problem with links, when you click on the like you it gives an error.
The links is added behind http://www.brahimusic.com/ which of course gives you a page not found.
Tx for your help.
An
Hey, nice code here, works really well! Just one question, is it possible to attach the ‘posted xx minutes ago’ segment to the end of this that keeps in sync with Twitter? Would quite like that for my own site… Thanks!
Thanks, I could easily integrate my latest tweet into my homepage with your code! Here is a small improvement to your code: if you use “$tweet = html_entity_decode($tweet)” after line 9, you don’t need to replace every single entity with str_replace(). :-D