Display Latest Tweet Code Snippet for WordPress

I took yoast’s code for the displaying the latest tweet and modified it so that it would work on my blog. For some reason the exact code on Yoast’s page didn’t work all the time for me. Please visit Yoasts blog linked previously for an explanation of the code. Here’s the code:

function display_latest_tweet () {
 require_once(ABSPATH . 'wp-includes/class-snoopy.php');
 $tweet   = get_option("lasttweet");
 $url  = "http://twitter.com/statuses/user_timeline/free_thinker.json?count=1";
 if ($tweet['lastcheck'] < ( mktime() - 60 ) ) {
 $snoopy = new Snoopy;
 $result = $snoopy->fetch($url);
 if ($result) {
 $twitterdata   = json_decode($snoopy->results,true);
 $i = 0;
 while ($twitterdata[$i]['in_reply_to_user_id'] != '') {
 $i++;
 }
 $pattern  = '/\@([a-zA-Z]+)/';
 $replace  = '<a href="http://twitter.com/'.<span class=">strtolower</a><a href="http://twitter.com/'.<span class=">('\1').'">@\1</a>';
 $output   = preg_replace($pattern,$replace,$twitterdata[$i]["text"]);

 $tweet['lastcheck'] = mktime();
 $tweet['data']    = $output;
 $tweet['rawdata']  = $twitterdata;
 $tweet['followers'] = $twitterdata[0]['user']['followers_count'];
 update_option('lasttweet',$tweet);
 if(contains_a_url($tweet['data'])) {
 $output = MakeItLink::transform($tweet['data']);
 } else
 $output = $tweet['data'];

 } else {
 echo "Twitter API not responding.";
 }
 } else {
 if(contains_a_url($tweet['data'])) {
 $output = MakeItLink::transform($tweet['data']);
 } else
 $output = $tweet['data'];
 }
 echo "".$output."";
}

function contains_a_url($data) {
 $input = $data;
 $regexp = "#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie";
 if(preg_match($regexp, $input, $matches)) {
 return TRUE;
 }
 else {
 return FALSE;
 }

}

class MakeItLink {
 function _link_www( $matches ) {
 $url = $matches[2];
 $url = MakeItLink::cleanURL( $url );
 if( empty( $url ) ) {
 return $matches[0];
 }

 return "{$matches[1]}<a href="{$<span class=&quot;hiddenSpellError&quot; pre=&quot;&quot;>url</span>}" target="_blank">{$url}</a>";
 }

 function cleanURL( $url ) {
 if( $url == '' ) {
 return $url;
 }

 $url = preg_replace( "|[^a-z0-9-~+_.?#=!&;,/:%@$*'()x80-xff]|i", '', $url );
 $url = str_replace( array( "%0d", "%0a" ), '', $url );
 $url = str_replace( ";//", "://", $url );

 /* If the URL doesn't appear to contain a scheme, we
 * presume it needs http:// appended (unless a relative
 * link starting with / or a php file).
 */
 if(
 strpos( $url, ":" ) === false
 && substr( $url, 0, 1 ) != "/"
 && !preg_match( "|^[a-z0-9-]+?.php|i", $url )
 ) {
 $url = "http://{$url}";
 }

 // Replace ampersands and single quotes
 $url = preg_replace( "|&([^#])(?![a-z]{2,8};)|", "&#038;$1", $url );
 $url = str_replace( "'", "&#039;", $url );

 return $url;
 }

 function transform( $text ) {
 $text = " {$text}";

 $text = preg_replace_callback(
 '#(?<=[\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#$%&~/\-=?@\[\](+]|[.,;:](?![\s<])|(?(1)\)(?![\s<])|\)))*)#is',
 array( 'MakeItLink', '_link_www' ),
 $text
 );

 $text = preg_replace( '#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $text );
 $text = trim( $text );

 return $text;
 }

}

To make sure that WP Super Cache doesn’t cache the tweet (I have had issues displaying the tweet, when it gets cached), I use the following code snippet as a function call to display_latest_tweet.


<!--mfunc display_latest_tweet() -->
<?php display_latest_tweet() ?>
<!--/mfunc-->

About Pratik Sinha

Linux Nerd, Socialist, Atheist, Adventuristic, Nature Lover, Geeky.

1 comment

Leave a Reply to rys Cancel reply