Today I am going to share one my experience in WordPress. Recently I have developed widget for one of my plugin Direction Map
.So I am going to explain how you can create own WordPress widget.
WordPress has a lot of very handy features and Widget is one of the same.Widget in WordPress is to add dynamic content and features to sidebars.so I must say widget is very good and useful feature of WordPress for technical and non-technical both users.
So, Let’s dive into the code
First of all, Create one file with the name of demo-widget
into your plugin or you can add your code into functions.php file or you can place below code into your plugin main file if you want to create widget for your plugin.
Step 1:
Here, In above code, I have just initialised my widget using action hook and will write widget code into the included file widget.php.
Step 2:
Now, Let’s create file widget.php at the same place or you want to have it.
In this file, first of call the following function
1 2 3 | register_widget( 'Demo_Widget' ); |
and next, extend the WP_Widget core class with Demo_Widget class
1 2 3 4 | class Demo_Widget extends WP_Widget { } |
Now,we will call constructor within class say,
1 2 3 4 5 6 7 8 9 10 | function Demo_Widget() { /* Widget settings. */ $widget_ops = array( 'classname' => 'Demo_Widget', 'description' => __('An example widget that displays a title', 'demo') ); /* Widget control settings. */ $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'demo-widget' ); /* Create the widget. */ $this->WP_Widget( 'demo-widget', __('Demo Widget', 'example'), $widget_ops, $control_ops ); } |
Step 3:
To display the widget on the screen, we will call below function into the class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function widget( $args, $instance ) { /* Our variables from the widget settings. */ $title = apply_filters('Demo', $instance['title'] ); /* Before widget (defined by themes). */ echo $before_widget; /* Display the widget title if one was input (before and after defined by themes). */ if ( $title ) echo $before_title . $title . $after_title; /* After widget (defined by themes). */ echo $after_widget; } |
Step 4:
To update any the widget settings, you need to call below function into class
1 2 3 4 5 6 7 8 9 | function update( $new_instance, $old_instance ) { $instance = $old_instance; /* Strip tags for title and name to remove HTML (important for text inputs). */ $instance['title'] = strip_tags( $new_instance['title'] ); return $instance; } |
Step 5:
Last but not the least, to displays the widget settings controls on the widget panel need to form elements and for that we can call following function into the class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function form( $instance ) { /* Set up some default widget settings. */ $defaults = array( 'title' => __('Demo', 'demo'), 'title' => __('Demo', 'demo'), 'width' => '200', 'height' => '300' ); $instance = wp_parse_args( (array) $instance, $defaults ); ?> <!-- Widget Title: Text Input --> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>"> <?php _e('Title:', 'hybrid'); ?> </label> <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" /> <!-- Your Name: Text Input --> <?php } ?> |
There we have it.Here is the demo of a widget which we have developed here.
DEMO
Download Demo
I hope this post was useful & it has helped you to create own widget in wordpress.Thanks for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (1)