-
-
Save bueltge/1305241 to your computer and use it in GitHub Desktop.
An example of how to build your own WordPress widget
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 | |
/* | |
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' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment