WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 13 months ago

#20542 new enhancement

Extend register_widget to allow passing params as second argument

Reported by: codearachnid Owned by: codearachnid
Milestone: Future Release Priority: normal
Severity: normal Version: 3.4
Component: Widgets Keywords: has-patch
Focuses: Cc:

Description

I ran into an issue recently where I needed to make multiple calls of register_widget('my_custom_class'); 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'); This does not allow for reuse of the same class to instantiate multiple widgets. I found that if I were to extend the register method in WP_Widget_Factory to pass through arguments into the extended WP_Widget constructor that I can reuse the same class for multiple widget registations. I am working out the logic to implement this directly into /wp-includes/widgets.php for future functionality improvement.

Attachments (2)

20542.diff (1.1 KB) - added by cmmarslender 13 months ago.
20542.2.diff (1.1 KB) - added by cmmarslender 13 months ago.
Adds underscore between class name and hash when there are args

Download all attachments as: .zip

Change History (9)

comment:1 @scribu3 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

comment:2 @kurtpayne3 years ago

  • Cc kpayne@… added

Can you explain more about the use case?

comment:3 @codearachnid3 years ago

The benefit is the ability to reuse the same class logic to instantiate multiple widgets (not talking about the current multiple use instances). A sample use case is in the http://wordpress.org/extend/plugins/widget-builder/ plugin where the same widget builder class is called multiple times with different widget parameters and by extending the widget factory was able to accomplish what I am proposing to patch in core.

comment:4 @toscho17 months ago

  • Cc info@… added

@cmmarslender13 months ago

comment:5 @cmmarslender13 months ago

  • Keywords has-patch added; needs-patch removed

@cmmarslender13 months ago

Adds underscore between class name and hash when there are args

comment:6 follow-up: @westonruter13 months ago

Re: 20542.diff, I've seen cases where a plugin expects the array keys of $wp_widget_factory->widgets to be the actual class names. That being said, they could (and perhaps should) be doing get_class() on the array values, but they'd need to be updated to do so.

comment:7 in reply to: ↑ 6 @cmmarslender13 months ago

Replying to westonruter:

Re: 20542.diff, I've seen cases where a plugin expects the array keys of $wp_widget_factory->widgets to be the actual class names. That being said, they could (and perhaps should) be doing get_class() on the array values, but they'd need to be updated to do so.

Unless $args is non-empty, the key is the name of the class, so this would still work for you average widget -Only time hash is added is if $args has a value of some sort

Note: See TracTickets for help on using tickets.