How to display your latest tweets in your WordPress blog without a plugin


Yes, I know. This article has been written a thousand times before. But this solution is different. It doesn’t require a plugin, and it’s very easy to implement. And if you have spare time to read this one, go ahead. I promise you it will be worth it.

Twitter is a social networking giant and it has become a part of everyone who loves to interact on the web. I have seen Twitter feeds on every WordPress blog that I’ve visited and I think it’s a very good way to keep your visitors and followers updated. I’ve also seen Twitter being implemented in different ways.

The Problem

So how do they do put up their twitter stream on their blogs, you might wonder. There are tons of tutorials out there describing and showing us exactly how to do it.

I’ve used some of the tutorials on my past projects and sometimes I get problems when the twitter search query feed returns nothing. One example is Ryan Barr’s Twitter Script. Don’t get me wrong, I love his script, it works very well but it uses the twitter search query as the feed his parser uses which sometimes gives empty results.

Below is an example of a Twitter search result query feed. If you try to view it, it’s empty. But if you check his profile, he has 3 entries. (thebrowneez is one of my unupdated Twitter profiles by the way)

The Solution

The solution is very obvious. We should use the user’s direct RSS feed. Click the RSS link below. It gives you results, right? And it never comes empty as long as the Twitter account is active.

If you are having trouble finding your twitter feed RSS, you can check out this article.

The only problem now is how do we parse and display the Twitter feed on our blog? I’ve got it covered so all you have to do is just copy and paste the code on your templates.

The Code

I added comments in between the lines of the codes so you have a better understanding on it. I used the fetch_feed() function which is a newer version of the deprecated wp_rss() function.

I’m no hardcore PHP programmer so if you ever find something that needs tweaking or optimization, please let me know. I hope this little bit of code will help you out on your next projects.

include_once(ABSPATH . WPINC . '/feed.php');

$username = "wordpress";
$feed = "";
$num = 5;

//this is a function which will convert text links to clickable links
function makeClickableLinks($text) {
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)','<a href="\\1">\\1</a>', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)','\\1<a href="http://\\2">\\2</a>', $text);
return $text;

$rss = fetch_feed($feed);
if (!is_wp_error( $rss ) ) :
     $maxitems = $rss->get_item_quantity($num);
     $rss_items = $rss->get_items(0, $maxitems);

     <?php if ($maxitems == 0) echo '<li>No items.</li>';
     foreach ( $rss_items as $item ) : ?>
          $tweet = str_replace($username.':','',$item->get_title()); //replaces the username which is displayed on the feed
          $tweet = makeClickableLinks($tweet); //converts text links to clickable links
          $tweet = preg_replace('#@([\\d\\w]+)#', '<a href="$1">$0</a>', $tweet);//converts hashtags to clickable links
          $tweet = preg_replace('/#([\\d\\w]+)/', '<a href="$1">$0</a>', $tweet);//converts @username to links
          echo $tweet . " <small><a href='".$item->get_permalink()."'>" . human_time_diff($item->get_date('U'), current_time('timestamp')) . " ago</a></small>";
     <?php endforeach; ?>

Well that’s it! I hope it helped you one way or another. All you have to do now is to create your own CSS styling.

I have other tutorials and tricks for WordPress that I’d like to share so keep coming back for new stuff. I’ll try to update this blog as often as I could. I’m thinking of adding new content at least thrice a month.

Tags: , , , ,


Wanna share your thoughts? Leave a message
  1. Gordon
    August 1st, 2010
    at 2:44 pm

    Thanks for the nice tutorial. Better approach than the others.

  2. max
    September 29th, 2010
    at 4:25 am

    great work, this works just fine

  3. jack d
    December 11th, 2010
    at 7:26 am

    Thank you this code just saved my life, after 4 hours of trying to wrangle together a javascript solution.

    My biggest issue was switch Twitter made from “id” to “Id_str” — this solution thankfully avoids that and works with the new Twitter “Snowflake” tweet IDs.

  4. works a treat. thanks.

  5. Can you edit this for ‘non wp websites’?

  6. thank you so much for the codes… it helped me a lot. worked right away…

  7. Thomas
    October 4th, 2012
    at 10:56 pm

    Hi! This code is awesome, works perfectly, thank you! :)

    I tried to modify it to show my favorite tweets and replace :$username.rss


    But alas, it doesn’t work…

    Also do you know how to show my recent tweets and below this first box, another one showing my favorites?

    Apparently I can’t redeclare ‘makeClickableLinks’…

    Thanks a lot for your help and for your great help with this code!

Find me on the Web

Other places you can find me

Daily Digest

Resource Sites for Inspiration

Extra Stuff


Search this Website

Looking for something specific?