WordPress Create A Widget Tutorial
- Article
- Comment
WordPress Create A Widget Tutorial for your easy understanding of creating new Widget for your theme and Plugin with the Help of this tutorial. Lets get into the topic to create a simple widget for your easy understanding.
Lets start by extending the WordPress built in Widget Class. It helps us to code and get a structured Widget. Here is a Sample code.
class KV_Simple_Widget extend WP_Widget { }// End of Class.
The above class must be hooked into your WordPress Plugin’s main file or Your theme ” functions.php “. Than the class will take care. And you need to hook this above class to create your widget with the help of following code.
add_action( 'widgets_init', create_function('','return register_widget("KV_Simple_Widget");'));
Now, Lets move to add information to your widget and make it to available on admin Widgets page.
class KV_Simple_Widget extends WP_Widget { function KV_Simple_Widget() { $this->WP_Widget( 'KV_Simple_Widget', 'KV Simple Widget', // kv name your widget here. array( 'classname' => 'KV_Simple_Widget', 'description' => 'A simple plugin to create Exchange rates widget.' ) //description to know about your widget. ); }
The above class requires one another function. Just hook this function to get view the Widget details on admin widget page.
function widget($args, $instance) { // kv widget sidebar output extract($args, EXTR_SKIP); echo $before_widget; // kv pre-widget code from theme print 'Your Widget Operation Goes here. ' ; echo $after_widget; // kv post-widget code from theme } } // Class ends here.
Thats it for simple class to create Widget.