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)
http://search.twitter.com/search.atom?q=from:thebrowneez&rpp=1
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.
http://twitter.com/statuses/user_timeline/25106430.rss
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.
<?php
include_once(ABSPATH . WPINC . '/feed.php');
//configuration
$username = "wordpress";
$feed = "http://twitter.com/statuses/user_timeline/685513.rss";
$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);
endif;
?>
<ul>
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
foreach ( $rss_items as $item ) : ?>
<li>
<?php
$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="http://twitter.com/$1">$0</a>', $tweet);//converts hashtags to clickable links
$tweet = preg_replace('/#([\\d\\w]+)/', '<a href="http://twitter.com/search?q=%23$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>";
?>
</li>
<?php endforeach; ?>
</ul>
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: hack, twitter, twitter hack, web development, WordPress

August 1st, 2010
at 2:44 pm
Thanks for the nice tutorial. Better approach than the others.
September 26th, 2010
at 6:21 pm
Thanks Gordon.
September 29th, 2010
at 4:25 am
great work, this works just fine
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.
December 20th, 2010
at 7:08 pm
works a treat. thanks.
January 4th, 2011
at 1:29 am
Can you edit this for ‘non wp websites’?
October 17th, 2011
at 1:09 pm
thank you so much for the codes… it helped me a lot. worked right away…