1. Widget Class name should be ended with ‘_Widget’ only.
2. Class should extends ‘WP_Widget’
3. Have four functions
a) __construct()
b) widget($args,$instance)
c) form($instance)
d) update($new_instance,$old_instance)
How to create WordPress widgets with demo by Anil Kumar Panigrahi
Class
class contains the name with ‘__Widget’ and extends the ‘WP_Widget’
Class FL_Widget extends WP_Widget{ }
__construct()
__construct() is constructor for this class widget. This code will be run every time widget is loaded, when activated and updated.
publicfunction __construct(){
parent::__construct( 'fl_widget',// Base ID 'Favorite Link Widget',// Name array('description'=> __('A Favorite Link Widget is developed by Anil Kumar Panigrahi','text_domain'),)// Args );
}
widget($args,$instance)
widget($args,$instance) function contains the code that will be rendered to the sidebar when widget is added and that will be visible to wbsite visitors.
form($instance) function contains the setting page on WordPress widget admin screen. That will be visible in the Appearance -> Widgets. This method called the
form and all controlls will appear when widget options are expanded
update($new_instance,$old_instance) function called when click on “Save” option in the settings page in the admin secion/screen. And those details will save
into database by options.
3 thoughts on “How to create WordPress widgets with demo”