Need a WordPress Expert?

Hire me!

Jun30

Show Your Latest Tweet In 5 Lines of Code

by John Kolbert | Posted in: WordPress

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.

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.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

25 Comments

  1. Cespur said:

    Nice one! I will probally be using this, thanks.

    on July 17, 2009 at 1:22 pm Reply

  2. Green said:

    Very helpful. looking at options currently.

    on August 3, 2009 at 9:03 pm Reply

  3. human3rror said:

    awesome.

    on August 11, 2009 at 3:06 pm Reply

  4. webmack said:

    it works the first time, btu when the cache time expires, the tweet stops showing. any way to fix this? I’m hosted on a windows server running IIS.

    on August 15, 2009 at 5:54 pm Reply

  5. Andrea said:

    cool, thanks… twitter's really hot right now, should get in the bus before it leaves! :)

    on September 10, 2009 at 10:56 pm Reply

  6. shuks said:

    Thank you for this. very helpful indeed. quick question. Any way to refresh the tweet less than 1 hour? Thanks again.

    on October 4, 2009 at 6:52 pm Reply

  7. Jo6891 said:

    Fantastic! V. helpful, Thankyou!

    on October 5, 2009 at 4:06 pm Reply

  8. inventory programs said:

    hmm.. good for you! ever since you are really great!

    on October 16, 2009 at 9:06 am Reply

  9. Patience said:

    Thank you so much..I've spent hours to try and get this to work!…now to find some CSS help.

    on October 30, 2009 at 6:53 am Reply

  10. fast software said:

    Thanx for the useful info this blog is very educative.

    on November 5, 2009 at 1:55 pm Reply

  11. @mandrill said:

    Is there a way to add reply, retweet, follow and DM buttons/links to this in a similarly easy and user friendly fashion? Cos that would be great :)

    on November 12, 2009 at 1:43 pm Reply

  12. Presentations Expert said:

    Nice sharing. Searching for this codes for long.

    on November 20, 2009 at 5:59 pm Reply

  13. Marko said:

    small and very preaty peace of code :)
    Thank you

    on November 25, 2009 at 6:12 am Reply

  14. Richard said:

    Awesome!!
    Thanks

    on January 10, 2010 at 7:07 am Reply

  15. car said:

    Excellent, been looking for this for a while, thanks!

    on January 14, 2010 at 5:23 pm Reply

  16. Naturalpills said:

    Coding, very good lines!!

    on January 18, 2010 at 3:37 pm Reply

  17. Oil Heater said:

    Great piece of code, thanks for sharing it!

    on February 8, 2010 at 12:20 pm Reply

  18. faydaliweb said:

    Thanks for the codes.But ı think this can be done with plugin who dont wants to edit the templates

    on February 23, 2010 at 10:01 am Reply


RSS feed for comments on this post.

Trackbacks on this post

Links to this post will be listed here. You an add your own by linking to the TrackBack URL

  1. 40+ Awesome Tutorials and Techniques For WordPress Theme Developers | tripwire magazine

    [...] Show Your Latest Tweet In 5 Lines of Code [...]

  2. Son Twitter İletinizi Sitenizde Gösterin | Eray USTA

    [...] 1 | Kaynak 2 [...]

  3. Wordpress Wednesday: 50 Links & Resources from around the web | Wordpress Guerrilla

    [...] Show your latest tweet in 5 lines of code [...]

  4. 40+ Awesome Tutorials and Techniques For WordPress Theme Developers | huibit05.com

    [...] Show Your Latest Tweet In 5 Lines of Code [...]

  5. Tutorial: Tweets ohne Plugin ausgeben (m… | Wordpress Lesezeichen

    [...] Tutorial: Tweets ohne Plugin ausgeben (mit RSS-Parser) http://www.johnkolbert.com/wordpress/show-your-latest-tweet-in-5-lines-of-code/ [...]

  6. The Ultimate Wordpress Developer Toolbox | tripwire magazine

    [...] Show Your Latest Tweet In 5 Lines of Code [...]

  7. 65 Of The Best WordPress Tutorials « Junkiee.Net

    [...] 60. Show Your Latest Tweet In 5 Lines Of Code [...]

Leave a comment



By pressing submit you are granting me a perpetual, non-exclusive licence to reproduce, paraphrase, and display your words, name, and/or website on this domain. All comments subject to moderation at my discretion.