--- title: How To Create Wordpress Widgets date: 2014-06-22 00:00:00 featured_image: https://images.unsplash.com/photo-1503925802536-c9451dcd87b5?q=90&fm=jpg&w=1000&fit=max excerpt: Wordpress provides a great API to create and interact with widgets. Widgets are a great way to allow plugin users to build their blog sidebar easily. I will show you how to create widgets, add widget options and display it in the sidebar. --- ![](https://images.unsplash.com/photo-1503925802536-c9451dcd87b5?q=90&fm=jpg&w=1000&fit=max) Wordpress provides a great API to create and interact with widgets. Widgets are a great way to allow plugin users to build their blog sidebar easily. I will show you how to create widgets, add widget options and display it in the sidebar. ### Creating a Widget In order to create a custom widget in wordpress, You need to extend `Wp_Widget` class and define your widget methods. Here's an overview of the class. ```php class clivern_custom_widget extends WP_Widget { function clivern_custom_widget(){ //process widget } function form($instance){ //show widget form in admin panel } function update($new_instance, $old_instance) { //update widget settings } function widget($args, $instance) { //display widget } } ``` To register your widget, you can use `widgets_init` action hook. This hook invoked after default widgets have been registered. Then you use `register_widget()` function to register your widget. Now it's time to register our new widget. This widget will be used to show advertisements on our blog sidebar. ```php function register_clivern_custom_widget(){ register_widget('clivern_custom_widget'); } add_action('widgets_init', 'register_clivern_custom_widget'); ``` As you can see, the `register_widget()` function accepts one parameter (widget class name) so widget class should be look like this. ```php class clivern_custom_widget extends WP_Widget { //... } ``` It is worth noting that widget class should be with unique name so you should always prefix this name with something unique like plugin name or brand name. Let's build the method that will process our widget. This method must have the same name of the class. ```php function clivern_custom_widget(){ //process widget $widget_options = array( 'classname'=> 'clivern_custom_widget_classname', 'description'=> 'A simple widget to show ads in blog sidebar.', ); $this->WP_Widget('clivern_custom_widget', 'Ads Widget', $widget_options); } ``` As you can see, first we created an array to store widget options (`$widget_options`). These options include `classname` and `description`. The `classname` added to widget element in front end and `description` displayed under the widget on widgets page. Then we passed the widget id (`clivern_custom_widget`), the widget name (`Ads Widget`) and an array of options (`$widget_options`) you set earlier to the `Wp_Widget`. Next we need to create widget form. Our widget will accept four values because it will be used to show four advertisements banners in blog sidebar. ```php function form($instance){ //show widget form in admin panel $default_settings = array( 'title' => 'Advertisements', 'ads_box_1'=>'', 'ads_box_2'=>'', 'ads_box_3'=>'', 'ads_box_4'=>'', ); $instance = wp_parse_args( (array) $instance, $default_settings ); $title = $instance['title']; $ads_box_1 = $instance['ads_box_1']; $ads_box_2 = $instance['ads_box_2']; $ads_box_3 = $instance['ads_box_3']; $ads_box_4 = $instance['ads_box_4']; ?>

Title:

Ads Box 1:

Ads Box 2:

Ads Box 3:

Ads Box 4:

  • '.$ads_box_1.'
  • '.$ads_box_2.'
  • '.$ads_box_3.'
  • '.$ads_box_4.'
  • '; echo $after_widget; } ``` First, we extracted the `$args` parameter. This variable holds another variables (`$before_widget` and `$after_widget`). You should always apply `widget_title` filter to widget title and output `$before_widget`, `$after_widget`, `$before_title` and `$after_title`. This enables other developers to modify your widget appearance. Let's take a look at full widget code. ```php function register_clivern_custom_widget(){ register_widget('clivern_custom_widget'); } add_action('widgets_init', 'register_clivern_custom_widget'); class clivern_custom_widget extends WP_Widget { function clivern_custom_widget(){ //process widget $widget_options = array( 'classname'=> 'clivern_custom_widget_classname', 'description'=> 'A simple widget to show ads in blog sidebar.', ); $this->WP_Widget('clivern_custom_widget', 'Ads Widget', $widget_options); } function form($instance){ //show widget form in admin panel $default_settings = array( 'title' => 'Advertisements', 'ads_box_1'=>'', 'ads_box_2'=>'', 'ads_box_3'=>'', 'ads_box_4'=>'', ); $instance = wp_parse_args( (array) $instance, $default_settings ); $title = $instance['title']; $ads_box_1 = $instance['ads_box_1']; $ads_box_2 = $instance['ads_box_2']; $ads_box_3 = $instance['ads_box_3']; $ads_box_4 = $instance['ads_box_4']; ?>

    Title:

    Ads Box 1:

    Ads Box 2:

    Ads Box 3:

    Ads Box 4:

  • '.$ads_box_1.'
  • '.$ads_box_2.'
  • '.$ads_box_3.'
  • '.$ads_box_4.'
  • '; echo $after_widget; } } ```