WP_Widget

Home » Classes » WP_Widget

The WP_Widget class is a fundamental class in WordPress used for creating custom widgets. Widgets are small blocks of content that can be placed in designated areas of a WordPress website, called widget areas or sidebars.

The WP_Widget class is an abstract class, meaning that it cannot be instantiated directly. Instead, developers need to extend this class to create their own custom widget classes. Extending the WP_Widget class allows developers to add their own functionality to the widget, such as custom settings, input fields, and output display.

The WP_Widget class contains many methods, but some of the most commonly used methods include:

  • __construct() – This method is called when the widget is instantiated and sets up the widget’s basic properties, such as its name and description.
  • widget() – This method is responsible for displaying the widget’s output on the front end of the website.
  • update() – This method is called when the widget is saved in the WordPress admin area and is responsible for updating the widget’s settings.

Here is an example usage of the WP_Widget class to create a custom widget:

class My_Custom_Widget extends WP_Widget {

  function __construct() {
    parent::__construct(
      'my_custom_widget',
      'My Custom Widget',
      array('description' => __('A custom widget that displays the latest posts.'))
    );
  }

  function widget($args, $instance) {
    $title = apply_filters('widget_title', $instance['title']);
    echo $args['before_widget'];
    echo $args['before_title'] . $title . $args['after_title'];
    // custom widget output code
    echo $args['after_widget'];
  }

  function update($new_instance, $old_instance) {
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    return $instance;
  }

  function form($instance) {
    $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
    ?>
    <p>
      <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
      <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>">
    </p>
    <?php 
  }
}

In this example, we are creating a custom widget class called My_Custom_Widget that extends the WP_Widget class. We define the widget’s name, description, and other properties in the __construct() method. The widget() method is responsible for displaying the widget’s output on the front end of the website, while the update() method is responsible for updating the widget’s settings. Finally, the form() method displays the widget’s settings form in the WordPress admin area.

This is just a basic example of how the WP_Widget class can be used to create custom widgets. With the WP_Widget class, developers have the flexibility to create widgets that can display any type of content they want and add custom functionality to meet their needs.

Learn More on WordPress.org

WordPress snippets using the WP_Widget class

Register an account to save your snippets or go Pro to get more features.