WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 5 months ago

#20542 closed enhancement (invalid)

Extend register_widget to allow passing params as second argument

Reported by: codearachnid Owned by: codearachnid
Milestone: Priority: normal
Severity: normal Version: 3.4
Component: Widgets Keywords:
Focuses: Cc:
PR Number:

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 6 years ago.
20542.2.diff (1.1 KB) - added by cmmarslender 6 years ago.
Adds underscore between class name and hash when there are args

Download all attachments as: .zip

Change History (12)

#1 @scribu
8 years ago

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

#2 @kurtpayne
8 years ago

  • Cc kpayne@… added

Can you explain more about the use case?

#3 @codearachnid
8 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.

#4 @toscho
6 years ago

  • Cc info@… added

@cmmarslender
6 years ago

#5 @cmmarslender
6 years ago

  • Keywords has-patch added; needs-patch removed

@cmmarslender
6 years ago

Adds underscore between class name and hash when there are args

#6 follow-up: @westonruter
6 years 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.

#7 in reply to: ↑ 6 @cmmarslender
6 years 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

#8 @chriscct7
4 years ago

  • Keywords needs-refresh added

#9 @westonruter
3 years ago

  • Keywords close reporter-feedback added

@codearachnid I believe this ticket is now invalid since you can pass in pre-instantiated widgets when calling register_widget(). See #28216. Please confirm.

#10 @audrasjb
5 months ago

  • Keywords has-patch needs-refresh close reporter-feedback removed
  • Milestone Future Release deleted
  • Resolution set to invalid
  • Status changed from new to closed

Hi there,

I'm going to close this ticket per Weston's comment above (poster 3years ago, not reporter feedback).

Feel free to reopen it if needed.

Cheers,
Jb

Note: See TracTickets for help on using tickets.