#53427 closed defect (bug) (fixed)
Widget preview not working if widget registered via a instance
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 5.8 | Priority: | normal |
Severity: | major | Version: | 5.8 |
Component: | REST API | Keywords: | has-patch |
Focuses: | rest-api | Cc: |
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.
4 years ago
#1
- Keywords has-patch added; needs-patch removed
This ticket was mentioned in PR #1378 on WordPress/wordpress-develop by spacedmonkey.
4 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.
4 years ago
#5
@
4 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
@
4 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.
4 years ago
#8
@
4 years ago
- Summary changed from Widget preivew not working if widget registered via a instance to Widget preview not working if widget registered via a instance
#9
@
4 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
@
4 years ago
- Owner set to noisysocks
- Status changed from new to 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
@
4 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
@
4 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.
Trac ticket: https://core.trac.wordpress.org/ticket/53427