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

Craig

August 31, 20109:33 am

Do I need to change my twitter settings? Your username successfully pulls in your last tweet but when I try mine I get nothing.

Reply

John Kolbert

August 31, 20108:12 pm

You shouldn’t need to. If you haven’t tweeted for a while (like days or weeks), it occasionally shows nothing. But otherwise it should just pull up your latest tweet.

Reply

Dan

September 6, 20104:36 pm

Hi John,

nice and simple script!

The script is working fine for me, but the feed is not cached!
I’m running WP 3.0.1.

Reply

Darfuria

September 10, 20103:13 pm

Anyone know how to grab the date of the tweet as well?

Reply

sub-zero

September 12, 20104:40 pm

Thanks I’ve been looking for a way to do this!

Reply

Richard

September 13, 20101:56 am

Hi John, thanks for the post. Does this code need to be updated in light of the recent change Twitter made to their API? I don’t know exactly what the change was but I do know that I had to re-add all my accounts to Tweetie. Do you know anything about that? Thanks.

Reply

adam

September 21, 20108:43 pm

You’d want to change where it says “atom_content” to “updated” so it would look like this (sorry if the code doesn’t come out great in this comment)

function wp_echoTwitterDate($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]['updated'];
}

That’s if you wanted to make like a second function to use along with it. You could save your server some time by just storing the two variables into a string and echoing the whole string instead of two functions but it’s whatever works.. explore! but that should help you get the date. Keep in mind it’s going to come out in some odd format so you may want to change it using something like this:

echo date(‘m/d/Y’,strtotime($tweet->items[0]['updated']));

instead of just echoing the raw data. Basically the strtotime() function changes the raw data into a timestamp and the date() function formats it how you’d like. If you want more on working with the date() function do a google search(php date). hope this helps!

Reply

Ash Robbins

September 28, 20102:53 pm

John Kolbert I love you!

I’ve been trying to work this out for a couple of hours and kept getting bulletted list items, but your solution works perfectly! Thanks

Ash

Reply

Mark Garity

October 8, 20101:40 pm

Strange. I get the error “rss.php is deprecated since version 3.0! Please use class-simplepie.php”

Reply

BrianK

November 24, 20107:16 am

I know you wrote this a while back, but want to let you know it’s awesome.

Reply

KevinD

December 29, 20105:54 pm

I’m trying this out but it seems to have an issue with tweets that contain a link. Any ideas on what I might be doing wrong?

Reply

Sean

January 5, 201112:22 am

Thanks for this, mate. Just what I was looking for. Does this support links @tags and #tags? If not how would I go about implementing it?

Reply

Moz

January 29, 20114:01 pm

Warning: I tried this code and it seems to have damaged the entire page. Do not use this code.

    Reply

    John Kolbert

    January 29, 20115:21 pm

    You must be using it incorrectly. I use this on many sites and just tried it on the latest nightly build of WordPress 3.1 and it’s working just fine.

Reply

Mike Key

February 1, 20112:01 pm

Yup, it works just fine on WordPress 3.1

Great little simple snippet.

Reply

Jim

February 2, 201111:35 pm

Absolutely a great solution! Thanks for this.

Only one thing, I’m looking for a way to reduce the number of characters of the outputted tweet and add … at the end of the tweet (so, three dots). How to do this?

Thanks
Jim

    Reply

    noush

    June 10, 20119:57 pm

    have you figured it out how to display specified number of words from the latest tweet with clickable arrow [>>] or dots [... ]to read furture?

Reply

Jay Pick

February 5, 201111:18 pm

Spot on! Cleanest solution I’ve found. Works a treat, thanks

Reply

Alasdair

February 6, 20114:12 pm

How would you get the date from the atom feed, with the “about a minute ago” etc. formats? Does Twitter send those out or would we have to do some coding to achieve it?

Great code though, thanks.

    Reply

    Alasdair

    February 6, 20114:13 pm

    Note to self: Read all comments next time! Ignore me!

Reply

Jim

February 11, 201111:30 pm

I’m using this version. I replaced the rss.php because it is depreciated and I added 3 dots at the end in case I have to shorten the tweet.

One thing I noticed with this script that when there is a @ in the tweet, the link stops at that point. This is the tweet I am talking about. http://twitter.com/SonyOnline/status/35522732640706560

This is the code:
http://jhnk.pastebin.com/EyN0Axpr

Reply

Bk Designs

February 15, 20119:26 am

Thanks for this I havent been able to pull twitter onto my website design and this does it professionaly and perfect!

Reply

Drew

February 25, 20115:36 pm

Simple solution – thanks

Reply

Daniel Hellier

March 10, 20115:01 am

Hey, I edited your code a little and well, yeah.. http://danielhellier.com/latest-tweets-function/

    Reply

    Elijah Paul

    March 25, 20114:09 am

    Hey Daniel,

    I edited your code a little further to display the time past since the tweet(s) rather than
    the date format. http://bit.ly/fkLWNB

Reply

Lena Shore

June 18, 20115:37 pm

[ X ] You Receive A Virtual Hug.

PERFECT! And no stupid plugin to make it happen! Fabulous!!! Thank you!!!! Works like a dream!

Reply

Jimmyjames

June 22, 20119:54 pm

THANK YOU!!!! I was getting so sick of Jetpack’s Twitter widget messing up.

Reply

Adem

July 21, 20111:37 pm

Works great! Much better than trying to get the masses of Twitter widgets working.

Reply

Sagive

August 13, 201110:41 pm

Thanks a lot man.. simple and easy to inegrate with my options page (building a template).
Cheers, Sagive

Reply

hankd

September 8, 20112:49 am

This code totally rocks. But I’m having an issue with it. I get everything to work fine with username A, but when I put in username B I get nothing. I switch it back to username A and everything works fine again. Does anyone know if there is a setting in twitter that needs to be set or unset in order to work? Thanks again for the sweet code.

    Reply

    Brian Krogsgard

    September 27, 20116:03 pm

    I had the same issue. It’s built using search, and if you don’t tweet for a couple weeks, Twitter drops it and you get nothing. Either tweet enough to support this method, or find another solution.

    I just adapted it to the other one posted here: http://danielhellier.com/latest-tweets-function/

Reply

Mark Schoones

October 12, 20119:57 am

How do i style the tweet with CSS? How do i link the php-script with the css?

Reply

Josh Stauffer

October 14, 20112:16 pm

Only problem for me is that the Search API only indexes the last 6-9 days of tweets. If a user doesn’t update often, zero results could be returned.

Other than that, this would work like a charm and I appreciate the efficient code. ;-)

Reply

johnny

December 4, 201112:31 am

simple. lean. effective.

thanks!

Reply

Jason King

December 8, 20112:56 pm

Thanks, seems to be working fine.

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)