Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Favorite Tweets widget
// favorite tweets
class rkv_fav_tweets extends WP_Widget {
function rkv_fav_tweets() {
$widget_ops = array( 'classname' => 'fav_tweets', 'description' => 'Displays tweets favorited by a user' );
$this->WP_Widget( 'fav_tweets', 'Favorite Tweets', $widget_ops );
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
$user = $instance['user'];
$count = $instance['count'];
$url = 'http://api.twitter.com/1/favorites/'.$user.'.json?count='.$count.'';
$trans = $user.'_twitter_favorites';
// check for stored transient. if none present, create one
if( false == get_transient( $trans ) ) {
$response = wp_remote_get ( $url );
// Save a transient to the database
set_transient($trans, $response, 60*60*12 );
} // end transient check
// check for transient cache'd result
$response = get_transient( $trans );
if( is_wp_error( $response ) ) {
echo 'There was an error retrieving data from twitter';
} else {
$output = json_decode( $response['body'] );
}
echo '<ul class="fav_tweets">';
foreach ($output as $tweet) {
// get variables
$id = $tweet->id_str;
$text = $tweet->text;
$when = $tweet->created_at;
$user = $tweet->user->screen_name;
$name = $tweet->user->name;
$image = $tweet->user->profile_image_url;
// set some defaults
$tweet_link = 'http://twitter.com/#!/'.$user.'/status/'.$id.'';
$user_link = 'http://twitter.com/#!/'.$user.'';
// clean up date
$stamp = strtotime ($when);
$date = date('M jS, Y', $stamp);
// build tweet
echo '<li>';
echo '<span class="twt_pic"><a href="'.$user_link.'" target="_blank"><img src="'.$image.'"></a></span>';
echo '<span class="twt_text"><a href="'.$tweet_link.'" target="_blank">'.$text.'</a></span>';
echo '<span class="twt_source">tweet via <a href="'.$user_link.'" target="_blank">'.$user.'</a> on '.$date.'</span>';
echo '</li>';
}
echo $after_widget;
?>
<?php }
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['user'] = strip_tags($new_instance['user']);
$instance['count'] = $new_instance['count'];
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$instance = wp_parse_args( (array) $instance, array(
'title' => 'Recent Favorites',
'user' => '',
'count' => '5',
));
$title = strip_tags($instance['title']);
$user = strip_tags($instance['user']);
$count = strip_tags($instance['count']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>">Widget Title:<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('user'); ?>">Twitter User:<input class="widefat" id="<?php echo $this->get_field_id('user'); ?>" name="<?php echo $this->get_field_name('user'); ?>" type="text" value="<?php echo esc_attr($user); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('count'); ?>">Tweet Count:<input class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo esc_attr($count); ?>" /></label></p>
<?php }
} // class
add_action( 'widgets_init', create_function( '', "register_widget('rkv_fav_tweets');" ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment