[wp-hackers] Extending WP_Widget_Factory

Timothy Wood codearachnid at gmail.com
Mon Apr 23 17:51:50 UTC 2012

I ran into an issue recently where I needed to make multiple calls of
register_widget on the same class and have multiple unique widgets
registered. The problem I ran into is that every time the same class
is called it will overwrite the last instance of the same class during
the widget constructor request parent::__construct('unique_id',
'widget title'); After pouring through the /wp-includes/widgets.php
code I found that if I were to extend the register method to pass
through arguments into the extended WP_Widget constructor that I can
reuse the same class for multiple widget registations. Currently I am
directly bypassing the register_widget(); method to extend the
WP_Widget_Factory class directly and instantiating it through a loop.
Here is a sample of what I am suggestion for extension of the widget
factory and that this would also be added into the register_widget()
method as a secondary argument.

class Extend_WP_Widget_Factory extends WP_Widget_Factory {

	// Overload register($widget_class) with ability to pass parameters
into widgets
	function register($widget_class, $param = null) {
		$this->widgets[$widget_class] = new $widget_class($param);


class Extend_Widget_Builder_Display extends WP_Widget {

		function Extend_Widget_Builder_Display($param = null) {
			$widget_ops = array( 'classname' => 'widge_' . $UniqueID);
			$control_ops = array( 'id_base' => 'widget_' . $UniqueID );
			parent::__construct( 'widget_' . $UniqueID, __($UniqueID,
'unique_domain'), $widget_ops, $control_ops );

Does anyone see why this wouldn't be a good idea to add into core as a
default option?

More information about the wp-hackers mailing list