Skip to content

Instantly share code, notes, and snippets.

@jaredatch
Created September 18, 2013 18:08
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 jaredatch/6613079 to your computer and use it in GitHub Desktop.
Save jaredatch/6613079 to your computer and use it in GitHub Desktop.
Sample widget template
<?php
/**
* Sample widget
*
* @since 1.0.0.0
*/
class ja_sample_Widget extends WP_Widget {
/**
* Holds widget settings defaults, populated in constructor.
*
* @since 1.0.0
* @var array
*/
protected $defaults;
/**
* Constructor
*
* @since 1.0.0
*/
function __construct() {
// widget defaults
$this->defaults = array(
'title' => '',
'text' => '',
);
// widget basics
$widget_ops = array(
'classname' => 'ja-sample-widget',
'description' => 'Widget description goes here.'
);
// widget controls
$control_ops = array(
'id_base' => 'ja-sample-widget',
'width' => 400,
);
// load widget
$this->WP_Widget( 'ja-sample-widget', 'JA Sample Widget', $widget_ops, $control_ops );
}
/**
* Outputs the HTML for this widget.
*
* @since 1.0.0
* @param array $args An array of standard parameters for widgets in this theme
* @param array $instance An array of settings for this widget instance
*/
function widget( $args, $instance ) {
extract( $args );
// Merge with defaults
$instance = wp_parse_args( (array) $instance, $this->defaults );
echo $before_widget;
// Title
if ( !empty( $instance['title'] ) ) {
echo $before_title . $instance['title'] . $after_title;
}
// Text
if ( !empty( $instance['text'] ) ) {
echo esc_html( $instance['text'] );
}
echo $after_widget;
}
/**
* Deals with the settings when they are saved by the admin. Here is
* where any validation should be dealt with.
*
* @since 1.0.0
* @param array $new_instance An array of new settings as submitted by the admin
* @param array $old_instance An array of the previous settings
* @return array The validated and (if necessary) amended settings
*/
function update( $new_instance, $old_instance ) {
$new_instance['title'] = strip_tags( $new_instance['title'] );
$new_instance['text'] = esc_html( $new_instance['text'] );
return $new_instance;
}
/**
* Displays the form for this widget on the Widgets page of the WP Admin area.
*
* @since 1.0.0
* @param array $instance An array of the current settings for this widget
*/
function form( $instance ) {
// Merge with defaults
$instance = wp_parse_args( (array) $instance, $this->defaults );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'text' ); ?>">Text:</label>
<input type="text" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" value="<?php echo esc_attr( $instance['text'] ); ?>" class="widefat" />
</p>
<?php
}
}
add_action( 'widgets_init', create_function( '', "register_widget('ja_sample_Widget');" ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment