How to Create Custom Widgets With custome functionality.

Step 1

Extend WP_widget class

class MyNewWidget extends WP_Widget () /* WP_Widget class located in wp-includes/widgets.php */

Step 2
function MyNewWidget() {
// Instantiate the parent object
//parent::__construct( false, ‘My New Widget Title’ );

$widget_ops = array(
‘classname’ => ‘postsfromcat’,
‘description’ => ‘Allows you to display a list of
thumbnail posts’);

$control_ops = array(
‘width’ => 250,
‘height’ => 250,
‘id_base’ => ‘widget-19_mynewwidget-6’);

$this->WP_Widget(‘widget-19_mynewwidget-6’, ‘thumbnail’, $widget_ops, $control_ops );
}

}

/*$widget_ops -> you cen set custom class name and description according to your widget.

$control_ops-> set widht, height and id of ur widget(widget id is created automaticaly,when your widget is created)

Step 3

function widget( $args, $instance ) {
// Widget output

extract($args); /*you need to extract argument from widget class.

$title = $instance[‘title’];¬† //get textbox value like this.

//write functionality,whatever you want.

}

Step  4

function update( $new_instance, $old_instance ) {
// Save widget options

//save value of textbox like this.

$instance[‘title’] = $new_instance[‘title’];
return $instance;

}

Step 5

function form( $instance ) {

// Output admin widget options form?>

//write HTML,whatever you want to show in widget.

}
}

Step 6

//register your custom widget

function myplugin_register_widgets() {
register_widget( ‘MyNewWidget’ );

}

//hook your custom widget with widget in it.

add_action( ‘widgets_init’, ‘myplugin_register_widgets’ );

 

 

 

contact us