WP_Widget_Factory::register() WordPress Method

The WP_Widget_Factory::register() method allows you to register a new widget class with the WordPress widget factory. This is useful if you want to create a new widget that can be used in WordPress themes.

WP_Widget_Factory::register( string|WP_Widget $widget ) #

Registers a widget subclass.


Parameters

$widget

(string|WP_Widget)(Required)Either the name of a WP_Widget subclass or an instance of a WP_Widget subclass.


Top ↑

Source

File: wp-includes/class-wp-widget-factory.php

	public function register( $widget ) {
		if ( $widget instanceof WP_Widget ) {
			$this->widgets[ spl_object_hash( $widget ) ] = $widget;
		} else {
			$this->widgets[ $widget ] = new $widget();
		}
	}


Top ↑

Changelog

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.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.