#53427 closed defect (bug) (fixed)
Widget preview not working if widget registered via a instance
| Reported by: | spacedmonkey | Owned by: | noisysocks |
|---|---|---|---|
| Priority: | normal | Milestone: | 5.8 |
| Component: | REST API | Version: | 5.8 |
| Severity: | major | Keywords: | has-patch |
| Cc: | Focuses: | rest-api |
Description
While testing the current WordPress 5.8 beta 2, I found an issue with widget preview.
The register_widget function can be called with a class name or a class instance. Once called with a class instance, the class instance is converted to hash as used key in array. See widget factory
public function register( $widget ) { if ( $widget instanceof WP_Widget ) { $this->widgets[ spl_object_hash( $widget ) ] = $widget; } else { $this->widgets[ $widget ] = new $widget(); } }
In the new widget type controller, this method only passed by class name.
private function get_widget_preview( $widget_object, $instance ) { ob_start(); the_widget( get_class( $widget_object ), $instance ); return ob_get_clean(); }
In the_widget function, there is no look for spl_object_hash.
if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) { _doing_it_wrong( __FUNCTION__, sprintf( /* translators: %s: register_widget() */ __( 'Widgets need to be registered using %s, before they can be displayed.' ), '<code>register_widget()</code>' ), '4.9.0' ); return; } $widget_obj = $wp_widget_factory->widgets[ $widget ]; if ( ! ( $widget_obj instanceof WP_Widget ) ) { return; }
Change History (18)
This ticket was mentioned in PR #1377 on WordPress/wordpress-develop by spacedmonkey.
5 years ago
#1
- Keywords has-patch added; needs-patch removed
This ticket was mentioned in PR #1378 on WordPress/wordpress-develop by spacedmonkey.
5 years ago
#3
Trac ticket: https://core.trac.wordpress.org/ticket/53427
This ticket was mentioned in Slack in #core-editor by desrosj. View the logs.
5 years ago
#5
@
5 years ago
Looks good @spacedmonkey!
src/wp-includes/blocks/legacy-widget.php (and all block PHP) is copied from @wordpress/widgets during grunt build --dev so we'll have to make those changes upstream in the Gutenberg repo instead:
#6
@
5 years ago
@noisysocks I have created a PR on the gutenberg repo - https://github.com/WordPress/gutenberg/pull/32781
This ticket was mentioned in Slack in #core by spacedmonkey. View the logs.
5 years ago
#8
@
5 years ago
- Summary Widget preivew not working if widget registered via a instance → Widget preview not working if widget registered via a instance
#9
@
5 years ago
Awesome. Let's wait until GB32781 is merged and back ported to Core and then we can commit everything except the src/wp-includes/blocks/legacy-widget.php changes that are in https://github.com/WordPress/wordpress-develop/pull/1378.
#10
@
5 years ago
- Owner set to
- Status new → assigned
Now that GB32781 is merged, should should be good merge this. We will have to update the widget package at the same time.
Not sure how this affects #53441
#11
@
5 years ago
I wanted to test this but I don't have any function-based widgets installed. Would you share the one you use for testing?
#12
@
5 years ago
@zieladam This patch doesn't fix function based widgets. It fixes an issue when if you pass an WP_Widget instance to register_widget it would not work.
I found this via a plugin I worked on called web-stories. See how we register our widget.
If you install our plugin, 1.8.0 or high you can help test.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Trac ticket: https://core.trac.wordpress.org/ticket/53427