register_widget( string|WP_Widget $widget )

Register a widget

Description

Registers a WP_Widget widget

See also

Parameters

$widgetstring|WP_Widgetrequired
Either the name of a WP_Widget subclass or an instance of a WP_Widget subclass.

Source

function register_widget( $widget ) {
	global $wp_widget_factory;

	$wp_widget_factory->register( $widget );
}

Changelog

VersionDescription
4.6.0Updated the $widget parameter to also accept a WP_Widget instance object instead of simply a WP_Widget subclass name.
2.8.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    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.