How To Display Your Latest Tweet On Your WordPress Blog

12th March 2010

How To Display Your Latest Tweet On Your WordPress Blog How To Display Your Latest Tweet On Your WordPress Blog

Displaying your latest tweet on your site can be a good way of advertising your Twitter account (to potentially get more followers) and can help show visitors that you’re active and still working on the site.

In this WordPress video tip I’ll show you how easy it is to add your latest tweet to your WordPress blog. There are a range of plugins that can potentially do this for you, but it’s actually very simple to do this yourself through writing just a few lines of code.

I’ve also included some video notes and the source code below the video – and since this is a video tip on Twitter, I should probably advertise my account – you can follow at @cpcreed :-)

Quick note: If you have any issues with seeing the source code in this video you might want to watch it in HD and full screen mode.

Video Notes

In this tutorial I’m going to use a test installation of the Saburo WordPress theme (screenshot below) with a view to adding my latest tweet to the sidebar.

The first thing you need to do is open your functions.php file – add the following code to the bottom of that file and save the changes:

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

This code is very simple – we’re basically creating a new function called “displayLatestTweet” which takes your Twitter username as an argument. We’re then fetching your latest tweet and storing it in a variable – the value of this variable is then displayed on the screen.

The next step is to open your sidebar.php file – add the following code:

<h2>Latest Tweet</h2>
<p><?php displayLatestTweet('cpcreed'); ?></p>

Again, this is very straightforward – we’re adding a title (“Latest Tweet”) and below that we’re calling the function we just created (displayLatestTweet) and passing in our Twitter user name (be sure to change the above code to use your own Twitter user name).

If you then view your site your latest tweet should now be displayed – you can see the final result in the Saburo theme below:

That’s it – very quick and simple to implement! I hope you found this tip useful and remember that you can follow on Twitter at @cpcreed.

Enjoy This Post? Please Consider Sharing :-)

  • Twitter
  • StumbleUpon
  • Facebook
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

18 Comments

  1. March 13th, 2010 at 3:16 pm

    Cool thx :) This is my way how to display your latest tweet in own template: http://damek.eu/statusy-twittera-we-wlasnym-szablonie/

  2. April 22nd, 2010 at 8:27 pm

    Chris:

    The best and easiest method of displaying Tweets on WordPress I’ve seen so far. I was wondering if you could help me with one problem, though…

    When the tweet displays on my blog, it spits out character entity codes.

    For example:
    ” I’m international now, baby! ”
    (turns into…)
    ” I'm international now, baby! ”
    on my blog page…

    Any recommendations on how to fix this?

    Thanks:)

    • April 23rd, 2010 at 9:04 am

      Hi Tone – I’ve not noticed this before – I’ll take a look into it.

      • October 11th, 2010 at 3:50 am

        I must have had too much red…. Are these 2 lines different? I have hooked me up a blog feed. Thank you for your efforts

  3. June 1st, 2010 at 6:45 pm

    There is only one problem… I can’t be found using Twitter Search! Any other way to get my twitter feed on my site?

    • John

      July 11th, 2011 at 1:27 pm

      Same here. Any alternate solutions? I love how simple this is and I can see it working on another username I tried but some usernames don’t appear in the search unfortunately.

  4. July 1st, 2010 at 12:08 am

    Cool! I have just got into blogs and tweeting and I am loving it. This tut has helped me get my tweets on my blog….I feel brainy! ;)

  5. Mia

    July 3rd, 2010 at 7:21 pm

    You are my hero :D ! – no seriously , I am now chiseling a slab of marble in your likeness :D
    Since the WP 3.0 upgrade – most plugins have become incompatible , and have caused serious issues w/ my theme ( and probably with many users themes out there ) ; I believe I have tried EVERY plugin and code out there for the past 3 days – and NONE of them have worked ! In fact : most of them cause my theme some serious issues . This is the only code which has worked like a charm :)

    Thank you so much Chris ! ( I will now head over to twitter & stalk you .. *cough..I mean add you ! ) :D

    M.

    • July 4th, 2010 at 9:52 am

      No problem Mia – glad it’s working :-) Always a pain when plugins become incompatible after updates!

  6. John

    July 5th, 2010 at 5:43 pm

    Thanks for this. Will this make links/hash marks clickable and open in a new window? If not, is there any way to implement it?

  7. August 26th, 2010 at 11:18 pm

    I love the apple icon Mia. Very cool!

  8. September 2nd, 2010 at 3:21 pm

    One question!

    If the twitter is unavailable what will happen? Does It show nothing or error?
    Does the blog works fine in this situation?

    Please answer!

    Thx!

  9. Nick

    September 6th, 2010 at 6:29 pm

    Its odd i used your code.. and replaced my twitter username it just simple dont work…

  10. November 12th, 2010 at 5:13 am

    really works, and thanks..

    ____________________
    from indonesia with love

  11. November 23rd, 2010 at 8:57 pm

    can i use this template on index.php?

  12. December 5th, 2010 at 6:29 am

    Thanks for sharing this! I think I could read it all day:)

  13. January 5th, 2011 at 10:24 am

    Thanks for sharing! I wish there would be more info on this.

  14. Hugo

    April 22nd, 2011 at 8:14 am

    This works very fine, thanks for sharing!

    Just one little but important questions: is it possible to show multiple tweets with your method?

Trackbacks/Pingbacks

  1. March 13th, 2010 at 5:25 am

    [...] User News if (typeof Meebo == 'undefined') { Meebo=function(){(Meebo._=Meebo._||[]).push(arguments)}; (function(q){ var args = arguments; if (!document.body) { return setTimeout(function(){ args.callee.apply(this, args) }, 100); } var d=document, b=d.body, m=b.insertBefore(d.createElement('div'), b.firstChild); s=d.createElement('script'); m.id='meebo'; m.style.display='none'; m.innerHTML=''; s.src='http'+(q.https?'s':'')+'://'+(q.stage?'stage-':'')+'cim.meebo.com/cim/cim.php?network='+q.network; b.insertBefore(s, b.firstChild); })({network:'tripwiremagazine_fu72bi'}); }How To Display Your Latest Tweet On Your WordPress Blog [...]

  2. March 13th, 2010 at 12:55 pm

    [...] How To Display Your Latest Tweet On Your WordPress Blog [...]

  3. March 15th, 2010 at 2:05 am

    [...] How To Display Your Latest Tweet On Your WordPress Blog [...]

  4. March 19th, 2010 at 11:52 am

    [...] und diese auf eurem Blog automatisch anzeigen lassen wollt, solltet Ihr die Webseite “vooshthemes.com” besuchen. Hier findet Ihr eine kleine Anleitung wie man die letzten Twitter Feeds [...]