Show Latest Tweets On WordPress
Today’s daily code will show you how to show your latest tweets in your WordPress theme.
Below you will find 3 different ways of showing your latest tweets by manually editing your theme files – the simple JS method, adding a function to your functions.php file and last a longer but super awesome PHP method for gathering customizing and showing your latest twitter tweets.
Personally I use the 3rd method on this blog but all methods work well and should be easy to use.
Simple JS Latest Tweets
1. Create a container for the tweets to appear
<ul id="twitter_update_list"> <li>Loading Tweets..</li> </ul>
2. Add the javascript to retrieve your latest tweets
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script> <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/zenverse.json?callback=twitterCallback2&count=5"></script>
Show Latest Tweet Using A Function
1. Add the following code to your functions.php file
<?php
function show_latest_tweet($twitterID){
include_once(ABSPATH.WPINC.'/rss.php');
$latest_tweet = fetch_rss("http://search.twitter.com/search.atom?q=from:" . $twitterID . "&rpp=1");
echo $latest_tweet->items[0]['atom_content'];
}
?>
2. Show the function wherever you want your tweets to appear (make sure to change wpexplorer with your twitter username)
<h4>Latest Tweets</h4>
<p><?php show_latest_tweet('wpexplorer'); ?></p>
Advanced PHP Method For Showing Latest Tweets
This is the method I currently use and I really love it. It allows for further customization of your tweets and uses straight PHP so it will be quicker then the JS method. This code is an altered version of the latest tweet PHP snippet provided by the awesome people at CreareGroup (see original source).
1. Add the full PHP code wherever you want your latest tweets to appear
<?php
function changeLink($string, $tags=false, $nofollow, $target){
if($tags){
$string = strip_tags($string);
} else {
if($target){
$string = str_replace("<a", "<a target=\"_blank\"", $string);
}
if($nofollow){
$string = str_replace("<a", "<a rel=\"nofollow\"", $string);
}
}
return $string;
}
function getLatestTweet($xml, $tags=false, $nofollow=true, $target=true){
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$x = $xmlDoc->getElementsByTagName("entry"); // get all entries
$tweets = array();
foreach($x as $item){
$tweet = array();
if($item->childNodes->length)
{
foreach($item->childNodes as $i){
$tweet[$i->nodeName] = $i->nodeValue;
}
}
$tweets[] = $tweet;
}
// if using bullets start UL here
echo "<ul>\n";
foreach($tweets as $tweettag){
/********************** Getting Times (Hours/Minutes/Days) */
$tweetdate = $tweettag["published"];
$tweet = $tweettag["content"];
$timedate = explode("T",$tweetdate);
$date = $timedate[0];
$time = substr($timedate[1],0, -1);
$tweettime = (strtotime($date." ".$time))+3600; // This is the value of the time difference - UK + 1 hours (3600 seconds)
$nowtime = time();
$timeago = ($nowtime-$tweettime);
$thehours = floor($timeago/3600);
$theminutes = floor($timeago/60);
$thedays = floor($timeago/86400);
/********************* Checking the times and returning correct value */
if($theminutes < 60){
if($theminutes < 1){
$timemessage = "Less than 1 minute ago";
} else if($theminutes == 1) {
$timemessage = $theminutes." minute ago.";
} else {
$timemessage = $theminutes." minutes ago.";
}
} else if($theminutes > 60 && $thedays < 1){
if($thehours == 1){
$timemessage = $thehours." hour ago.";
} else {
$timemessage = $thehours." hours ago.";
}
} else {
if($thedays == 1){
$timemessage = $thedays." day ago.";
} else {
$timemessage = $thedays." days ago.";
}
}
echo "<li>".changeLink($tweet, $tags, $nofollow, $target)."<br />\n";
echo "<span>".$timemessage."</span></li>\n";
}
echo "</ul>\n";
// if using bullets end UL here
}
// Usage (XML FEED, STRIP_TAGS (DEFAULT NO), NOFOLLOW (DEFAULT YES), TARGETBLANK (DEFAULT YES))
$tweetxml = "http://search.twitter.com/search.atom?q=from:wpexplorer&rpp=1";
// Step 4 - Usage (XML FEED, STRIP_TAGS (DEFAULT NO), NOFOLLOW (DEFAULT YES), TARGETBLANK (DEFAULT YES))
getLatestTweet($tweetxml, false, true, true);
?>
2. Edit to suit your needs. Make sure to specifically change where it says “wpexplorer” to your user name and where it says “rpp=1″ to change the value to the amount of tweets you want to show.



