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


Reply

Cespur

July 17, 20091:22 pm

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

Reply

Green

August 3, 20099:03 pm

Very helpful. looking at options currently.

Reply

human3rror

August 11, 20093:06 pm

awesome.

Reply

webmack

August 15, 20095:54 pm

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.

Reply

Andrea

September 10, 200910:56 pm

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

Reply

shuks

October 4, 20096:52 pm

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

Reply

Jo6891

October 5, 20094:06 pm

Fantastic! V. helpful, Thankyou!

Reply

inventory programs

October 16, 20099:06 am

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

Reply

Patience

October 30, 20096:53 am

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

Reply

fast software

November 5, 20091:55 pm

Thanx for the useful info this blog is very educative.

Reply

@mandrill

November 12, 20091:43 pm

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

Reply

Presentations Expert

November 20, 20095:59 pm

Nice sharing. Searching for this codes for long.

Reply

Marko

November 25, 20096:12 am

small and very preaty peace of code :)
Thank you

Reply

Richard

January 10, 20107:07 am

Awesome!!
Thanks

Reply

car

January 14, 20105:23 pm

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

Reply

Naturalpills

January 18, 20103:37 pm

Coding, very good lines!!

Reply

Oil Heater

February 8, 201012:20 pm

Great piece of code, thanks for sharing it!

Reply

faydaliweb

February 23, 201010:01 am

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

    Reply

    John Kolbert

    March 18, 201010:33 pm

    Yes, but I’m a developer. I like to do things myself rather then rely on plugins. :)

Reply

SJL Website Design

March 16, 20106:02 pm

Thanks for the great piece of code John, very simple solution compared to the javascript alternatives.

Reply

Fire Curtains

April 28, 20104:11 pm

Cheers for this, im gonna go put our twitter feed on our blog now!

Reply

Chris

April 30, 20105:57 am

Sweet! Thanks so much. This worked great for me. I needed something that would show the latest tweet per user, based on a custom field. Your 5 lines of code saved me hours of trying to find a plugin that could easily be changed to fit our needs. Copy, paste & 30 seconds of editing to throw in some variables & now we have complete functionality.

Thanks again!!

Reply

Brian

June 1, 20109:25 pm

Is there a way to show more than 1 tweet and maybe add a time stamp? This truly is the best option for WP. Kudos, sir.

Reply

JP

June 25, 20103:32 pm

nice article :)

Reply

rys

July 13, 20105:59 am

you can also refer to this site on how to display your latest tweets in twitter. with twitter API.
as easy as 1 2 3.. ;)

http://www.ryscript.co.cc/web/how-to-display-latest-tweets-using-javascript-and-twitter-api/

thanks..

Reply

justin

July 14, 20104:47 pm

does this work with wordpress 3.0? I put the above code in my theme’s functions.php file and it breaks wordpress.

    Reply

    John Kolbert

    July 28, 20103:59 am

    I’ve used this successfully in WP 3.0.

Reply

Sanjay

July 23, 20108:39 am

Thanks for the code. Is there any way to show more than 1 tweet?

Reply

John Kolbert

July 28, 20103:56 am

Here’s an updated version that allows for multiple tweets:

http://jhnk.pastebin.com/5A4aXYTW

Comment on this post:

License: By commenting you are granting me a perpetual, non-exclusive license to reproduce, paraphrase, and display your words, name, and/or website on this domain. All comments subject to moderation, editing, or deletion at my discretion.

Important: Unfortunately, I'm unable to offer support via comments. However, feel free to hire me.

Entering code?

(optional)