How to Create a Simple Widget on WordPress
WordPress widgets are classes, which makes them easy to duplicate, even if you’ve never worked with object-oriented programming before. A class provides a blueprint for objects that will be reused in various ways. Creating a new widget is a matter of copying the basic widget class, then adjusting the enclosed functions to output the content you have in mind. The widget class contains four functions: one that registers the widget, one that prints the widget output, one that updates the widget options, and one that displays the options form..
To create a widget, you only need to extend the standard WP_Widget class and some of its functions.
That base class also contains information about the functions that must be extended to get a working widget.
Open functions.php and add these lines:
class My_Widget extends WP_Widget {
public function __construct() {
// widget actual processes
}
public function form( $instance ) {
// outputs the options form on admin
}
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
}
public function widget( $args, $instance ) {
// outputs the content of the widget
}
}
register_widget( 'My_Widget' );

we first create a class. For example class derived from class WP_Widget My_Widget
then we fill in the construct or the initial value that will be processed by a class. Use thefunction __ construct ().
public function __construct() {
// widget actual processes
parent::WP_Widget(false,’Title Widget’,'description=Description Widget’);
}

Then the functions used by a widget from wordpress is
function form (), function update (), function widget ().
- Function form()
public function form( $instance ) {
// outputs the options form on admin
echo ‘include html coding in here’;
}In this function serves to display widget on the page menu backend.
2. Function update()
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
}
In this function serves to save the options in the database.
3. Function widget()
public function widget( $args, $instance ) {
// outputs the content of the widget
}
In this function serves to display the results on the web client.
Then register_widget serves to register a class to the widget WordPress.


