Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Twitter Widget Error WordPress

  • SOLVED

I am using a Twitter widget which works 99% of the time but ocassionally getting this error, which I believe occurs when Twitter is overloaded.

<blockquote>PHP Fatal error: Cannot use object of type WP_Error as array in /path/to/theme/name/widgets/twitter.php on line 35</blockquote>

Here is line 35:
$twitterdata = json_decode($result['body'], true);

Here is the full widget code:
function widget($args, $instance) {
extract($args, EXTR_SKIP);

// Set Defaults
$title = empty($instance['title']) ? 'Latest Tweet' : apply_filters('widget_title', $instance['title']);
$username = empty($instance['twitter_username']) ? 'Username' : apply_filters('widget_twitter_username', $instance['twitter_username']);
$tweets = empty($instance['number_of_tweets']) ? '1' : apply_filters('widget_number_of_tweets', $instance['number_of_tweets']);

echo $before_widget;
if ($title) echo '<h3 class="'.'widget-title">' . $title . "</h3>";

// Include http.php (Required)
if(!class_exists('WP_Http')) include_once(ABSPATH.WPINC.'/class-http.php');

$tweet = get_option("lasttweet");
$url = "http://twitter.com/statuses/user_timeline/".$username.".json?count=20";

// Tweets are cached to reduce load time and will only make a new API call if the cache is more than 1 minute old
if ($tweet['lastcheck'] < (mktime() - 60)) {

$request = new WP_Http;

$result = $request->request($url);

// Check if any tweets were returned from Twitter API
if ($result) {

$twitterdata = json_decode($result['body'], true);
$i = 0;
while ($twitterdata[$i]['in_reply_to_user_id'] != '') { $i++; }

// Parse @id
$output = preg_replace('/@(\w+)/', '@<a href="http://twitter.com/$1" class="at" target="_blank">$1</a>', $twitterdata[$i]["text"]);

// Parse #hashtag
$output = preg_replace('/\s#(\w+)/', ' <a href="http://twitter.com/#!/search?q=%23$1" class="hashtag" target="_blank">#$1</a>', $output);

// Friendly Date Format
$diff = time() - strtotime($twitterdata[$i]['created_at']);

$second = 1;
$minute = $second * 60;
$hour = $minute * 60;
$day = $hour * 24;
$week = $day * 7;

if (is_nan($diff) || $diff < 0) $created_at = "BLANK"; // Return blank string if unknown
else if ($diff < $second * 2) $created_at = "right now"; // within 2 seconds
else if ($diff < $minute) $created_at = floor($diff / $second) . " seconds ago";
else if ($diff < $minute * 2) $created_at = "1 minute ago";
else if ($diff < $hour) $created_at = floor($diff / $minute) . " minutes ago";
else if ($diff < $hour * 2) $created_at = "1 hour ago";
else if ($diff < $day) $created_at = floor($diff / $hour) . " hours ago";
else if ($diff > $day && $diff < $day * 2) $created_at = "yesterday";
else if ($diff < $day * 365) $created_at = floor($diff / $day) . " days ago";
else $created_at = "over a year ago";

// Cache Tweet Data
$tweet['lastcheck'] = mktime();
$tweet['data'] = $output;
$tweet['created_at'] = $created_at;
$tweet['rawdata'] = $twitterdata;
$tweet['followers'] = $twitterdata[$i]['user']['followers_count'];
update_option('lasttweet',$tweet);
} else {
echo "Twitter API not responding.";
}
} else {
$output = $tweet['data'];
}

// Output Tweet
$output = make_clickable($output);
echo '<p>'.$output.'</p>';
echo '<p><small>'.$tweet['created_at'].' - by <a href="http://www.twitter.com/'.$username.'">'.$username.'</a></small></p>';

echo $after_widget;
}


I would like a fix to this please. Any other improvement suggestions also welcome and if valuable to, I will add a bonus.

Answers (1)

2011-04-01

Ivaylo Draganov answers:

Try changing the line:


if ($result) {


to


if ($result && !is_wp_error($result)) {


drew comments:

Fantastic, thanks. =)


Ivaylo Draganov comments:

If it's a WP Error indeed you might wanna see the actual error message. So then change the line after <em>} else {</em> to that:


echo $result->get_error_message();


If wanna test whether it works, pass it real WP Error object. Change this:


$result = $request->request($url);


to that:


$result = new WP_Error('broke', __('Houston, we have a problem!'));


Cheers :)