[How to] Display Latest Tweet on WordPress
To display your latest tweet on your site just add the following function in your functions.php
file.
function stf_get_latest_tweet($username){ $tweet = get_option("stf_lasttweet"); $url = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1"; if ($tweet['lastcheck'] < ( mktime() - 60 ) ) { $feed = file_get_contents( $url ); $stepOne = explode("<content type="html">", $feed); $stepTwo = explode("</content>", $stepOne[1]); $output = $stepTwo[0]; $output = htmlspecialchars_decode ( $output , ENT_QUOTES ); // Decode all HTML entities $tweet['lastcheck'] = mktime(); $tweet['data'] = $output; update_option( 'stf_lasttweet', $tweet ); } else { $output = $tweet['data']; } return $output; }
This function returns your latest tweet as html. To display it on your site use this call anywhere you like in your theme:
<?php echo stf_get_latest_tweet("shailancom"); ?>
Enjoy!
Code SnippetsWordPress Tipsdisplayfunctions.phplatestphpSnippetstheme-designtweetWordpressWordpress Tips