Skip to content

Instantly share code, notes, and snippets.

@kprimdal
Created November 6, 2012 15:08
Show Gist options
  • Save kprimdal/4025302 to your computer and use it in GitHub Desktop.
Save kprimdal/4025302 to your computer and use it in GitHub Desktop.
WP: Widget Boilerplate
<?php
class Foo_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'foo_widget', // Base ID
'Foo_Widget', // Name
array( 'description' => __( 'A Foo Widget', 'text_domain' ), ) // Args
);
}
public function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Widget Name', 'widget-name-locale' ) : $instance['title'], $instance, $this->id_base);
echo $after_widget;
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
// TODO Update the widget with the new values
// Note that this 'Title' is just an example
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
public function form( $instance ) {
// TODO define default values for your variables
$instance = wp_parse_args(
(array) $instance,
array(
'title' => __( 'Widget Name', 'widget-name-locale' ),
)
);
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'widget-name-locale' ) ?></label>
<input type="text" class="widefat" value="<?php esc_attr_e( $instance['title'] ); ?>" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" />
</p>
<?php
}
}
// TODO remember to change 'leje_widget_taxonomy' to match the class name definition
add_action( 'widgets_init', create_function( '', 'register_widget( "foo_widget" );' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment