Skip to content

Instantly share code, notes, and snippets.

@ryanburnette
Created November 24, 2013 18:41
Show Gist options
  • Save ryanburnette/7630685 to your computer and use it in GitHub Desktop.
Save ryanburnette/7630685 to your computer and use it in GitHub Desktop.
A WordPress Widget Example/Template. Based on example from Pippin's Plugins. http://pippinsplugins.com/simple-wordpress-widget-template/
<?php
class example_widget extends WP_Widget {
function example_widget() {
parent::WP_Widget(false, $name = 'Example Text Widget');
}
function widget($args, $instance) {
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
$message = $instance['message'];
?>
<?php echo $before_widget; ?>
<?php if ( $title )
echo $before_title . $title . $after_title; ?>
<ul>
<li><?php echo $message; ?></li>
</ul>
<?php echo $after_widget; ?>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['message'] = strip_tags($new_instance['message']);
return $instance;
}
function form($instance) {
$title = esc_attr($instance['title']);
$message = esc_attr($instance['message']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('message'); ?>"><?php _e('Simple Message'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('message'); ?>" name="<?php echo $this->get_field_name('message'); ?>" type="text" value="<?php echo $message; ?>" />
</p>
<?php
}
}
add_action('widgets_init', create_function('', 'return register_widget("example_widget");'));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment