Created
June 20, 2012 21:17
-
-
Save krogsgard/2962293 to your computer and use it in GitHub Desktop.
tweets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add function to widgets_init that'll load our widget. | |
* @since 0.1 | |
*/ | |
add_action( 'widgets_init', 'latest_load_widgets' ); | |
/** | |
* Register our widget. | |
* 'Latest_Tweet_Widget' is the widget class used below. | |
* | |
* @since 0.1 | |
*/ | |
function latest_load_widgets() { | |
register_widget( 'Latest_Tweet_Widget' ); | |
} | |
/** | |
* Latest Tweet Widget class. | |
* This class handles everything that needs to be handled with the widget: | |
* the settings, form, display, and update. Nice! | |
* | |
* @since 0.1 | |
*/ | |
class Latest_Tweet_Widget extends WP_Widget { | |
/** | |
* Widget setup. | |
*/ | |
function Latest_Tweet_Widget() { | |
/* Widget settings. */ | |
$widget_ops = array( 'classname' => 'latest-tweet', 'description' => __('A social widget that displays your latest Tweet from Twitter.', 'latest-tweet') ); | |
/* Widget control settings. */ | |
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'latest-tweet-widget' ); | |
/* Create the widget. */ | |
$this->WP_Widget( 'latest-tweet-widget', __('Latest Tweet', 'latest-tweet'), $widget_ops, $control_ops ); | |
} | |
/** | |
* How to display the widget on the screen. | |
*/ | |
function widget( $args, $instance ) { | |
extract( $args ); | |
/* Our variables from the widget settings. */ | |
$title = apply_filters('widget_title', $instance['title'] ); | |
$twitter = $instance['twitter']; | |
$numbtweets = $instance['numbtweets']; | |
/* Before widget (defined by themes). */ | |
echo $before_widget; | |
/* Display the widget title if one was input (before and after defined by themes). */ | |
if ( $title ) | |
echo $before_title . $title . $after_title; | |
/* Display name from widget settings if one was input. */ | |
if ( $twitter ) { | |
/* latest tweet snippet by John Kolbert */ | |
function recentTweets($username, $numbtweets){ | |
include_once(ABSPATH.WPINC.'/rss.php'); | |
$tweet = fetch_feed("http://twitter.com/statuses/user_timeline/" . $username . ".rss" ); | |
if (!is_wp_error( $tweet ) ) : | |
$maxitems = $tweet->get_item_quantity($numbtweets); | |
$rss_items = $tweet->get_items(0, $maxitems); | |
endif; | |
if ($maxitems == 0) echo '<li>No longer Tweet. Sorry (:</li>'; | |
else $row = false; foreach ($rss_items as $item) { | |
$content = make_clickable(html_entity_decode($item->get_content())); | |
$content = str_replace($username.": ", "", $content); | |
$link = html_entity_decode($item->get_permalink()); | |
$date = $item->get_date('c'); | |
echo "<p".(($row = !$row)?' class="row2"':'')."><a href='$link' target='_blank'>$username: </a>$content</p>"; | |
} | |
} | |
recentTweets('' . $twitter . '', '1'); | |
} | |
/* After widget (defined by themes). */ | |
echo $after_widget; | |
} | |
/** | |
* Update the widget settings. | |
*/ | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
/* Strip tags for title and name to remove HTML (important for text inputs). */ | |
$instance['title'] = strip_tags( $new_instance['title'] ); | |
$instance['twitter'] = strip_tags( $new_instance['twitter'] ); | |
$instance['numbtweets'] = strip_tags( $new_instance['numbtweets'] ); | |
return $instance; | |
} | |
/** | |
* Displays the widget settings controls on the widget panel. | |
* Make use of the get_field_id() and get_field_name() function | |
* when creating your form elements. This handles the confusing stuff. | |
*/ | |
function form( $instance ) { | |
/* Set up some default widget settings. */ | |
$defaults = array( 'title' => __('Latest Tweet', 'latest-tweet'), 'twitter' => __('photomatt', 'latest-tweet'), 'numb tweets' => __('1', 'latest-tweet')); | |
$instance = wp_parse_args( (array) $instance, $defaults ); ?> | |
<!-- Widget Title: Text Input --> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label> | |
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" /> | |
</p> | |
<!-- Your Twitter: Text Input --> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'twitter' ); ?>"><?php _e('Your Twitter profile:', 'latest-tweet'); ?></label> | |
<input id="<?php echo $this->get_field_id( 'twitter' ); ?>" name="<?php echo $this->get_field_name( 'twitter' ); ?>" value="<?php echo $instance['twitter']; ?>" style="width:100%;" /> | |
</p> | |
<?php | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment