register_widget( string $widget_class )

Register a widget


Description Description

Registers a WP_Widget widget


Parameters Parameters

$widget_class

(string) (Required) The name of a class that extends WP_Widget


Top ↑

Source Source

File: wp-includes/widgets.php

function register_widget($widget_class) {
	global $wp_widget_factory;

	$wp_widget_factory->register($widget_class);
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.8.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

    Example

    <?php
    /**
     * Class WPDocs_New_Widget
     */
    class WPDocs_New_Widget extends WP_Widget {
    
    	/**
    	 * Constructs the new widget.
    	 *
    	 * @see WP_Widget::__construct()
    	 */
    	function __construct() {
    		// Instantiate the parent object.
    		parent::__construct( false, __( 'My New Widget Title', 'textdomain' ) );
    	}
    
    	/**
    	 * The widget's HTML output.
    	 *
    	 * @see WP_Widget::widget()
    	 *
    	 * @param array $args     Display arguments including before_title, after_title,
    	 *                        before_widget, and after_widget.
    	 * @param array $instance The settings for the particular instance of the widget.
    	 */
    	function widget( $args, $instance ) {}
    
    	/**
    	 * The widget update handler.
    	 *
    	 * @see WP_Widget::update()
    	 *
    	 * @param array $new_instance The new instance of the widget.
    	 * @param array $old_instance The old instance of the widget.
    	 * @return array The updated instance of the widget.
    	 */
    	function update( $new_instance, $old_instance ) {
    		return $new_instance;
    	}
    
    	/**
    	 * Output the admin widget options form HTML.
    	 *
    	 * @param array $instance The current widget settings.
    	 * @return string The HTML markup for the form.
    	 */
    	function form( $instance ) {
    		return '';
    	}
    }
    
    add_action( 'widgets_init', 'wpdocs_register_widgets' );
    
    /**
     * Register the new widget.
     *
     * @see 'widgets_init'
     */
    function wpdocs_register_widgets() {
    	register_widget( 'WPDocs_New_Widget' );
    }
    

You must log in before being able to contribute a note or feedback.