ControlledChaos
5/7/2015 - 8:35 AM

Widget Template

Widget Template

<?php

add_action( 'widgets_init', 'PREFIX_register_widgets' );

function PREFIX_register_widgets(){
  register_widget( 'WP_Widget_FOOBAR' );
}
class WP_Widget_FOOBAR extends WP_Widget {
	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		$widget_args = array( 'classname' => 'widget_FOOBAR', 'description' => __( "Widget description", PREFIX_TEXTDOMAIN ) );
		parent::__construct( 'WIDGET-ID', __( 'Widget title', PREFIX_TEXTDOMAIN ), $widget_args );
	}
	/**
	 * Front-end display of widget.
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	function widget( $args, $instance ) {
		extract( $args );
		$title = ! empty( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : false;
		echo $before_widget;
		if ( $title ) echo $before_title . $title . $after_title;
		// do widget stuff here
		echo $after_widget;
	}
	/**
	 * Back-end widget form.
	 *
	 * @param array $instance Previously saved values from database.
	 */
	function form( $instance ) {
?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>">
				<?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($instance['title']); ?>" />
			</label>
		</p>
<?php
	}
	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 */
	function update( $new_instance, $old_instance ) {
		
		$sanitized = array();
		foreach ( $new_instance as $key => $val ) {
			$sanitized[ $key ] = sanitize_text_field( $new_instance[ $key ] );
		}
		return $sanitized;
	}
}

?>

Widget Template

WordPress Snippet