Skip to content

Instantly share code, notes, and snippets.

@srdjan-m
Last active January 27, 2017 15:34
Show Gist options
  • Save srdjan-m/16d464af3b3ae253ce159954075bbbfd to your computer and use it in GitHub Desktop.
Save srdjan-m/16d464af3b3ae253ce159954075bbbfd to your computer and use it in GitHub Desktop.
wordpress: widget template from https://codex.wordpress.org/Widgets_API
// *** Extends the following class: src/wp-includes/class-wp-widget.php
class My_Widget extends WP_Widget {
/**
* Sets up the widgets name etc
*/
public function __construct() {
$widget_ops = array(
'classname' => 'my_widget',
'description' => 'My Widget is awesome',
);
parent::__construct( 'my_widget', 'My Widget', $widget_ops );
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
// outputs the content of the widget
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form( $instance ) {
// outputs the options form on admin
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*/
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
}
}
// *** To register the widget:
add_action( 'widgets_init', function(){
register_widget( 'My_Widget' );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment