Display your latest tweet without a plug-in

July 29, 2010 Shashank Agarwal Blog 1 minute, 54 seconds Edit This Article

8 in 15 Useful Twitter Hacks and Plug-Ins For WordPress If people like your blog, they would probably also enjoy your tweets. Displaying your latest tweets on your WordPress blog is a good way to gain new subscribers. A plug-in can do that, but for such a simple task, I prefer a hack. This one grabs your latest tweet and displays it on your blog.
This ready-to-use code can be pasted anywhere in your theme files. Just don’t forget to change the value of the $username on line 4. The $prefix and $suffix variable can be used to insert a title, and the div element can be used for further CSS styling.

// Your twitter username.<br></br>$username = "TwitterUsername";<br></br><br></br>// Prefix - some text you want displayed before your latest tweet.<br></br>// (HTML is OK, but be sure to escape quotes with backslashes: for example href="link.html")<br></br>$prefix = "<h2>My last Tweet</h2>";<br></br><br></br>// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)<br></br>$suffix = "";<br></br><br></br>$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";<br></br><br></br>function parse_feed($feed) {<br></br>    $stepOne = explode("<content type=""html"">", $feed);<br></br>    $stepTwo = explode("</content>", $stepOne[1]);<br></br>    $tweet = $stepTwo[0];<br></br>    $tweet = str_replace("<", "<", $tweet);<br></br>    $tweet = str_replace(">", ">", $tweet);<br></br>    return $tweet;<br></br>}<br></br><br></br>$twitterFeed = file_get_contents($feed);<br></br>echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);<br></br>?>

Save the file, and your latest tweet is displayed on your blog. Nice, huh?

Go to our new site- shankee.com