Hi!
I have been looking for a good solution to integrate latest tweets of a particular account into my sidebar, without a plugin.
I have tried the solution given by Joost De Valk here:http://yoast.com/display-latest-tweet/
Here is my code:
In my functions.php:
// get latest tweet and number of followers
function get_tweet_info(){
require_once(ABSPATH . 'wp-includes/class-snoopy.php');
$tweet = get_option("lasttweet");
$url = "http://twitter.com/statuses/user_timeline/jeherve.json?count=20";
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/'.strtolower('\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);
} else {
echo "Twitter API not responding.";
}
} else {
$output = $tweet['data'];
}
echo "<p>\"".$output."\"</p>";
};
add_action('middlesidebar', 'get_tweet_info');
and my hook in my sidebar of course:
<?php do_action('middlesidebar'); ?>
For some reason, it is still really slow.
Would there be a mistake in my code, or would there be a better solution?
Thanks!