Skip to content

Instantly share code, notes, and snippets.

@iamcanadian1973
Created July 11, 2014 00:46
Show Gist options
  • Save iamcanadian1973/160b27191d60626ee7ff to your computer and use it in GitHub Desktop.
Save iamcanadian1973/160b27191d60626ee7ff to your computer and use it in GitHub Desktop.
Skeleton widget
// Creating the widget
class KR_WIDGET extends WP_Widget {
const WIDGET_DOMAIN = 'fusemail';
const WIDGET_ID = 'kr_widget';
const WIDGET_NAME = 'KR Widget';
const WIDGET_DESCRIPTION = 'KR Widget Description.';
const WIDGET_TITLE = '';
function __construct() {
parent::__construct(
// Base ID of your widget
WIDGET_ID,
// Widget name will appear in UI
__( WIDGET_NAME, WIDGET_DOMAIN),
// Widget description
array( 'description' => __( WIDGET_DESCRIPTION, WIDGET_DOMAIN ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// Widget Output goes here
// After Widget
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( WIDGET_TITLE, WIDGET_DOMAIN );
}
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class wpb_widget ends here
// Register and load the widget
function kr_load_widget() {
register_widget( 'KR_WIDGET' );
}
add_action( 'widgets_init', 'kr_load_widget' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment