bueltge
10/21/2011 - 11:17 PM

An example of how to build your own WordPress widget

An example of how to build your own WordPress widget

<?php
/*
Plugin Name: FB Widget Tutorial
Plugin URI:  
Description: How to create WordPress Widgets.
Version:     13/03/2013
Author:      Frank Bültge
Author URI:  http://bueltge.de/
License:     GPLv3
*/

class Fb_Widget_Tutorial extends WP_Widget {
	
	function Fb_Widget_Tutorial() {
		
		$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 {
			// 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', 'fb_register_tutorial_widget' );
function fb_register_tutorial_widget() {
	
	register_widget( 'Fb_Widget_Tutorial' );
}