WordPress Create A Widget Tutorial - Kvcodes
X

Congrats, You are Subscribed to Receive Updates.

WordPress Create A Widget Tutorial


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.

kv_simple_widget

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.

 

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, OpenCart Theme Development, PHP, HTML, CSS, jQuery, Bootstrap development and content SEO.

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar

Subscribe E-mail Updates

Congrats, You are Subscribed to Receive Updates.

Advertisement

Category Posts