Show Your Latest Tweet In 5 Lines of Code

This tutorial shows you how to add your latest Tweet to your WordPress website. It’s fast and easy, and lets you broadcast your Twitter activity to your readers.

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){
     $tweet = fetch_rss("" . $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.


If you’d like to show more then one tweet, here is an updated version of this function:

Leave a Reply

Your email address will not be published. Required fields are marked *