Skip to content

Instantly share code, notes, and snippets.

@toekneestuck
Created September 25, 2012 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toekneestuck/3782559 to your computer and use it in GitHub Desktop.
Save toekneestuck/3782559 to your computer and use it in GitHub Desktop.
Simple Twitter Widget for WordPress
/**
* Twitter Widget
*
*/
class GT_Social_Twitter extends WP_Widget {
function GT_Social_Twitter(){
$widget_ops = array( 'classname' => 'twitter', 'description' => _x('A widget for displaying your recent tweets.', 'gt') );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'gt-twitter' );
parent::WP_Widget( false, _x('Recent Tweets', 'gt'), $widget_ops);
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => _x('Most Recent Tweets', 'gt'), 'id'=>'', 'number'=>3, 'btn_text'=> _x('Follow Us', 'gt') )); ?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title', 'gt'); ?></label><br />
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php if ( isset($instance['title']) ) echo $instance['title']; ?>" class="widefat" />
</p>
<p><label for="<?php echo $this->get_field_id( 'id' ); ?>"><?php _e('Twitter user:', 'gt'); ?></label><br />
<input id="<?php echo $this->get_field_id( 'id' ); ?>" name="<?php echo $this->get_field_name( 'id' ); ?>" value="<?php if ( isset($instance['id']) ) echo $instance['id']; ?>" class="widefat" />
</p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e('Number of tweets:', 'gt'); ?></label><br />
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php if ( isset($instance['number']) ) echo $instance['number']; ?>" class="widefat" />
</p>
<p><label for="<?php echo $this->get_field_id( 'btn_text' ); ?>"><?php _e('Button Text:', 'gt'); ?></label><br />
<input id="<?php echo $this->get_field_id( 'btn_text' ); ?>" name="<?php echo $this->get_field_name( 'btn_text' ); ?>" value="<?php if ( isset($instance['btn_text']) ) echo $instance['btn_text']; ?>" class="widefat" />
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Create widget settings instances. */
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['id'] = $new_instance['id'];
$instance['number'] = $new_instance['number'];
$instance['btn_text'] = $new_instance['btn_text'];
return $instance;
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', $instance['title'] );
$id = str_replace(array('&nbsp;', '@'), '', $instance['id']);
$btn_text = $instance['btn_text'];
$number = $instance['number'] ? $instance['number'] : 3;
/* Widget template */
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title;
// Get the feed if it's not cached
if( false === ($feed = get_transient('gt_twitter_feed')) ){
$user = $id;
$result = wp_remote_get("http://api.twitter.com/1/statuses/user_timeline.json?include_rts=1&screen_name=". $user ."&count=" . $number);
$feed = wp_remote_retrieve_body( $result );
// If the Twitter rate limit hasn't failed, set transients
if( strpos($feed, '{"error') === false ){
set_transient('gt_twitter_feed', $feed, 900);
set_transient('gt_twitter_feed_fallback', $feed, 86400);
// Fallback to an old value.
}else{
$feed = get_transient('gt_twitter_feed_fallback');
}
}
if ( $feed ) :
$tweets = json_decode($feed, true);
echo '<ol>';
$tweet_counter = 0;
foreach($tweets as $tweet):
$tweet_counter++;
$pattern = '/\@(\w+)/';
$replace = '<a rel="external nofollow" href="http://twitter.com/$1">@$1</a>';
$tweet['text'] = preg_replace($pattern, $replace , $tweet['text']);
$tweet['text'] = make_clickable($tweet['text']);
echo '<li>';
echo '<span class="tweet-content">';
echo $tweet['text'];
echo '</span> ';
echo '<span class="tweet-date"><a rel="external nofollow" href="http://twitter.com/'.$id.'/statuses/'. $tweet['id'].'/">';
echo 'about ' . human_time_diff( strtotime($tweet['created_at'])) . ' ago';
//echo date("F j, g:i A",strtotime($tweet['created_at']));
echo '</a></span>';
echo '</li>';
endforeach;
echo '</ol>';
echo "<a href='http://twitter.com/$id' rel='external nofollow' class='btn-blue right'>$btn_text</a>";
else:
echo '<p>Error requesting the latest tweets. Please try again later.</p>';
endif;
echo $after_widget;
}
}
/* Register widgets */
function gt_register_widgets() {
register_widget( 'GT_Social_Twitter' );
}
/* Load widget with widget_init function */
add_action( 'widgets_init', 'gt_register_widgets' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment