Title: WP_Widget_Factory
Published: April 25, 2014
Last modified: April 28, 2025

---

# class WP_Widget_Factory {}

## In this article

 * [Methods](https://developer.wordpress.org/reference/classes/WP_Widget_Factory/?output_format=md#methods)
 * [Source](https://developer.wordpress.org/reference/classes/WP_Widget_Factory/?output_format=md#source)
 * [Changelog](https://developer.wordpress.org/reference/classes/WP_Widget_Factory/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/classes/WP_Widget_Factory/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/classes/WP_Widget_Factory/?output_format=md#wp--skip-link--target)

Singleton that registers and instantiates [WP_Widget](https://developer.wordpress.org/reference/classes/wp_widget/)
classes.

## 󠀁[Methods](https://developer.wordpress.org/reference/classes/WP_Widget_Factory/?output_format=md#methods)󠁿

| Name | Description | 
| [WP_Widget_Factory::__construct](https://developer.wordpress.org/reference/classes/wp_widget_factory/__construct/) | PHP5 constructor. | 
| [WP_Widget_Factory::_register_widgets](https://developer.wordpress.org/reference/classes/wp_widget_factory/_register_widgets/) | Serves as a utility method for adding widgets to the registered widgets global. | 
| [WP_Widget_Factory::get_widget_key](https://developer.wordpress.org/reference/classes/wp_widget_factory/get_widget_key/) | Returns the registered key for the given widget type. | 
| [WP_Widget_Factory::get_widget_object](https://developer.wordpress.org/reference/classes/wp_widget_factory/get_widget_object/) | Returns the registered [WP_Widget](https://developer.wordpress.org/reference/classes/wp_widget/) object for the given widget type. | 
| [WP_Widget_Factory::register](https://developer.wordpress.org/reference/classes/wp_widget_factory/register/) | Registers a widget subclass. | 
| [WP_Widget_Factory::unregister](https://developer.wordpress.org/reference/classes/wp_widget_factory/unregister/) | Un-registers a widget subclass. | 
| [WP_Widget_Factory::WP_Widget_Factory](https://developer.wordpress.org/reference/classes/wp_widget_factory/wp_widget_factory/) | PHP4 constructor. — deprecated |

## 󠀁[Source](https://developer.wordpress.org/reference/classes/WP_Widget_Factory/?output_format=md#source)󠁿

    ```php
    class WP_Widget_Factory {

    	/**
    	 * Widgets array.
    	 *
    	 * @since 2.8.0
    	 * @var array
    	 */
    	public $widgets = array();

    	/**
    	 * PHP5 constructor.
    	 *
    	 * @since 4.3.0
    	 */
    	public function __construct() {
    		add_action( 'widgets_init', array( $this, '_register_widgets' ), 100 );
    	}

    	/**
    	 * PHP4 constructor.
    	 *
    	 * @since 2.8.0
    	 * @deprecated 4.3.0 Use __construct() instead.
    	 *
    	 * @see WP_Widget_Factory::__construct()
    	 */
    	public function WP_Widget_Factory() {
    		_deprecated_constructor( 'WP_Widget_Factory', '4.3.0' );
    		self::__construct();
    	}

    	/**
    	 * Registers a widget subclass.
    	 *
    	 * @since 2.8.0
    	 * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
    	 *              instead of simply a `WP_Widget` subclass name.
    	 *
    	 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
    	 */
    	public function register( $widget ) {
    		if ( $widget instanceof WP_Widget ) {
    			$this->widgets[ spl_object_hash( $widget ) ] = $widget;
    		} else {
    			$this->widgets[ $widget ] = new $widget();
    		}
    	}

    	/**
    	 * Un-registers a widget subclass.
    	 *
    	 * @since 2.8.0
    	 * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
    	 *              instead of simply a `WP_Widget` subclass name.
    	 *
    	 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
    	 */
    	public function unregister( $widget ) {
    		if ( $widget instanceof WP_Widget ) {
    			unset( $this->widgets[ spl_object_hash( $widget ) ] );
    		} else {
    			unset( $this->widgets[ $widget ] );
    		}
    	}

    	/**
    	 * Serves as a utility method for adding widgets to the registered widgets global.
    	 *
    	 * @since 2.8.0
    	 *
    	 * @global array $wp_registered_widgets
    	 */
    	public function _register_widgets() {
    		global $wp_registered_widgets;
    		$keys       = array_keys( $this->widgets );
    		$registered = array_keys( $wp_registered_widgets );
    		$registered = array_map( '_get_widget_id_base', $registered );

    		foreach ( $keys as $key ) {
    			// Don't register new widget if old widget with the same id is already registered.
    			if ( in_array( $this->widgets[ $key ]->id_base, $registered, true ) ) {
    				unset( $this->widgets[ $key ] );
    				continue;
    			}

    			$this->widgets[ $key ]->_register();
    		}
    	}

    	/**
    	 * Returns the registered WP_Widget object for the given widget type.
    	 *
    	 * @since 5.8.0
    	 *
    	 * @param string $id_base Widget type ID.
    	 * @return WP_Widget|null
    	 */
    	public function get_widget_object( $id_base ) {
    		$key = $this->get_widget_key( $id_base );
    		if ( '' === $key ) {
    			return null;
    		}

    		return $this->widgets[ $key ];
    	}

    	/**
    	 * Returns the registered key for the given widget type.
    	 *
    	 * @since 5.8.0
    	 *
    	 * @param string $id_base Widget type ID.
    	 * @return string
    	 */
    	public function get_widget_key( $id_base ) {
    		foreach ( $this->widgets as $key => $widget_object ) {
    			if ( $widget_object->id_base === $id_base ) {
    				return $key;
    			}
    		}

    		return '';
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-widget-factory.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-widget-factory.php#L17)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-widget-factory.php#L17-L141)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/WP_Widget_Factory/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.4.0](https://developer.wordpress.org/reference/since/4.4.0/) | Moved to its own file from wp-includes/widgets.php | 
| [2.8.0](https://developer.wordpress.org/reference/since/2.8.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/classes/WP_Widget_Factory/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/classes/WP_Widget_Factory/?output_format=md#comment-content-993)
 2.   [Lance Cleveland](https://profiles.wordpress.org/charlestonsw/)  [  10 years ago  ](https://developer.wordpress.org/reference/classes/wp_widget_factory/#comment-993)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_widget_factory%2F%23comment-993)
    Vote results for this note: -1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_widget_factory%2F%23comment-993)
 4. Until it gets ported over, here is the Widgets API docs in Codex:
     [https://codex.wordpress.org/Widgets_API](https://codex.wordpress.org/Widgets_API)
 5.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_widget_factory%2F%3Freplytocom%3D993%23feedback-editor-993)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_widget_factory%2F)
before being able to contribute a note or feedback.