Display recent Tweets in your WordPress site

Image by Luc Latulippe
More and more corporate sites are using Twitter these days and so you’re more likely to have a client who requires some sort of Twitter activity to be shown in his site. Perhaps he wants his most recent tweet displayed, or a few results from a Twitter search feed.
There are a number of Twitter based widgets and plugins, but in most cases, they’re heavily styled and don’t make it very easy for you to integrate a tweet or two into a very specific area in your design. The script below let’s you do just that and so it’s my method of choice.
Here’s a great and flexible script to grab some recent tweets for your WordPress site:
<?php
$doc = new DOMDocument();
# load the RSS -- replace 'lylo' with your user of choice
if($doc->load('http://twitter.com/statuses/user_timeline/INSERT_USERNAME_HERE.rss')) {
echo "<ul>\n";
# number of <li> elements to display. 20 is the maximum
$max_tweets = 10;
$i = 1;
foreach ($doc->getElementsByTagName('item') as $node) {
# fetch the title from the RSS feed.
# Note: 'pubDate' and 'link' are also useful (I use them in the sidebar of this blog)
$tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;
# the title of each tweet starts with "username: " which I want to remove
$tweet = substr($tweet, stripos($tweet, ':') + 1);
# OPTIONAL: turn URLs into links
$tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',
'<a href="$1">$1</a>', $tweet);
# OPTIONAL: turn @replies into links
$tweet = preg_replace("/@([0-9a-zA-Z]+)/",
"<a href=\"http://twitter.com/$1\">@$1</a>",
$tweet);
echo "<li>". $tweet . "</li>\n";
if($i++ >= $max_tweets) break;
}
echo "</ul>\n";
}
?>
Remember that if your working of a busy site, it’s probably a good idea to integrate this with a caching system to avoid any problems with the Twitter API limits.



you can also refer to this site on how to show the latest tweets in your blog site using tweeter API
. as easy as 1 2 3
http://www.ryscript.co.cc/web/how-to-display-latest-tweets-using-javascript-and-twitter-api/
thanks
Welldone, very very nice and helpful post….
thanks for sharing!
good bye
Thanks for this, Iv been looking for a solution for this for a while. Hopefully it will do what I need to to. I will let you know how I get on. Thansk for sharing!
Ahh perfect! Thanks for this, its just what I was looking for! Cheers
Howdy thanks for the code. I’ve tried like 5 different codes and couldn’t get them to work. Something weird with twitter I think. But I was wondering if there is away to add the date to the tweets? Thanks again.