Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created October 11, 2011 22:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save chrisguitarguy/1279630 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/1279630 to your computer and use it in GitHub Desktop.
An example of how to build your own WordPress widget
<?php
/*
Plugin Name: PMG Widget Tututorial
Plugin URI: http://pmg.co/category/wordpress
Description: How to create WordPress Widgets.
Version: 1.0
Author: Christopher Davis
Author URI: http://pmg.co/people/chris
License: GPL2
*/
class pmgTutTweetWidget extends WP_Widget
{
function pmgTutTweetWidget()
{
$opts = array(
'classname' => 'pmg-tweet-widget',
'description' => __( 'Display your most recent tweet' )
);
parent::__construct( 'pmg-tweet-widget', __( 'Latest Tweet' ), $opts );
}
function form( $instance )
{
$defaults = array(
'title' => '',
'twitter' => 'agencypmg',
);
$instance = wp_parse_args( $instance, $defaults );
$display = array(
'twitter' => __( 'Your Latest Tweet' ),
'custom' => __( 'A Custom Mesage' )
);
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title' ); ?></label>
<input type="text" name="<?php echo $this->get_field_name( 'title' ); ?>" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'twitter' ); ?>"><?php _e( 'Twitter' ); ?></label>
<input type="text" name="<?php echo $this->get_field_name( 'twitter' ); ?>" class="widefat" id="<?php echo $this->get_field_id( 'twitter' ); ?>" value="<?php echo esc_attr( $instance['twitter'] ); ?>" />
</p>
<?php
}
function update( $new, $old )
{
$clean = $old;
$clean['title'] = isset( $new['title'] ) ? strip_tags( esc_html( $new['title'] ) ) : '';
$clean['twitter'] = isset( $new['twitter'] ) ? esc_attr( $new['twitter'] ) : '';
return $clean;
}
function widget( $args, $instance )
{
extract( $args );
echo $before_widget;
if( $instance['title'] )
{
echo $before_title . strip_tags( $instance['title'] ) . $after_title;
}
?>
<div class="pmg-tweet-tweet">
<?php echo $this->get_tweet( esc_attr( $instance['twitter'] ) ); ?>
</div>
<?php
echo $after_widget;
}
/**
* Get our tweet from the database or from twitter;
*/
function get_tweet( $user )
{
/**
* Try to fetch our tweet from the database so we don't have to hit the
* Twitter api
*/
if( $tweet = get_transient( 'apex_tweet_' . $user ) )
{
return $tweet;
}
else // no dice, gotta go for twitter
{
// build our url, can't pass strings directly to wp_remote_get :/
$url = 'http://api.twitter.com/1/users/show.json?id=' . $user;
// fetch!
$resp = wp_remote_get( $url );
// bail if we failed to get a 200 response
if( is_wp_error( $resp ) || 200 != $resp['response']['code'] ) return '';
// parse the json
$obj = json_decode( $resp['body'] );
// get the status and make any links clickable
$status = isset( $obj->status->text ) ? make_clickable( $obj->status->text ) : '';
// Set our transient so we don't have to hit twitter again for 12 hours.
set_transient( 'apex_tweet_' . $user, $status, 60 * 60 * 12 );
// Whew, return the tweet
return $status;
}
}
} // end class
// Register our widget
add_action( 'widgets_init', 'pmg_add_tut_widget' );
function pmg_add_tut_widget()
{
register_widget( 'pmgTutTweetWidget' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment