Tutorial: Display your Latest Tweet on your Website

Get our posts emailed to you with our monthly newsletter, subscribe here.

UPDATE – I have posted a more recent ‘part two’ post which uses SimplePie to retrieve your Tweets. Check out the post: Tutorial: Display your Latest Tweet on your Website using SimplePie

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….

[php] $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 = “https://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(“&lt;”, “<“, $tweet);
$tweet = str_replace(“&gt;”, “>”, $tweet);
return $tweet;
}

$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?&gt;
[/php]

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:

[php light=”true”] echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
[/php]

to this:

[php light=”true”] $tweet = stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
[/php]

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!

Posted by Jason Bayless

32 Comments

  1. Hi there, I’m trying to get this on a simple html page, but I find no way to do it… is it possible using your code?
    Thanks in advance!!!

  2. I’m getting some Asian porn site when I click on your Part two link up top. Am I the only one?

    1. Thanks Stef, I guess Tr.im went down, the link has been updated I appreciate you letting me know! :)

  3. Hmm… works again now.

  4. Hmm… the implementation suddenly stopped working. Trying to retrieve the search feed that is used doesn’t work with Google Reader either… only a very old tweet is showing up.

    Any idea where the problem might be? Have there been changes to the twitter feed structure?

  5. I just search, and found your articles. Keen to try, and thanks, i just implement to my website. But when i try to tweet from twitter, need time to show in my website. But anyway, thanks for tutorial.

  6. 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

  7. 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!

  8. 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

  9. Ah and how can I display the timing of the tweet?

  10. Hi, I used your code on http://www.brahimusic.com
    But my question is how can I display a ‘ instead of it saying ‘

  11. No problemo Stu, everything works out great. I just read your post and actually implemented it already. hehe. thank you very much!

  12. @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.

  13. 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!

  14. @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

  15. 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.

  16. 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. :)

  17. 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!

  18. The post is now live, using SimplePie is a much better option – http://tr.im/yxMF

  19. 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

  20. 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!

  21. Last post went all weird.. shout if you need me help, either here or on my site

  22. 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?

  23. Very nice m8! Just got one question for ya, how do I format the text? Cos CSS doesnt seem to work :S

  24. 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

  25. 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

  26. @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

  27. mattcouchman 2009-07-11 at 14:44

    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 :)

  28. I used another Drupal snippet in the meantime.. but thanks!

  29. 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

  30. @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 :)

  31. Is it possible to show the last 5 tweets instead of only the last one?

Comments are closed.