Opened 7 years ago
Closed 7 years ago
#41743 closed defect (bug) (fixed)
Using the_widget on a widget that has not been registered results in an undefined index notice.
Reported by: | SeBsZ | Owned by: | obenland |
---|---|---|---|
Milestone: | 4.9 | Priority: | normal |
Severity: | normal | Version: | 2.8 |
Component: | Widgets | Keywords: | good-first-bug has-patch |
Focuses: | Cc: |
Description
First of all, this may be by design to notify the developer that they've probably made a mistake.
However, I'd like to suggest a small check that prevents this notice.
In Wordpress 4.8.1 wp-includes\widgets.php on line 1039 (or the second line inside the the_widget() function) there is:
$widget_obj = $wp_widget_factory->widgets[$widget];
if ( ! ( $widget_obj instanceof WP_Widget ) ) {
return;
}
This means that calling the_widget with a $widget name that has not been registered, instantly results in an undefined index notice.
A simple check would solve this:
if ( ! ( isset($wp_widget_factory->widgets[$widget]) ) ) {
return;
}
$widget_obj = $wp_widget_factory->widgets[$widget];
if ( ! ( $widget_obj instanceof WP_Widget ) ) {
return;
}
Apologies if this is not a bug but by design.
Attachments (4)
Change History (20)
This ticket was mentioned in Slack in #core by desrosj. View the logs.
7 years ago
#5
@
7 years ago
- Keywords has-patch added; needs-patch removed
Hi @obenland and @SeBsZ
I have tested the patch. And it is working fine.
Also, I have cleaned up the code according to WP Coding Standards given here: https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/
Cheers!
#6
@
7 years ago
Great, glad to have helped.
BTW, you cleaned up my code (added some spaces), however the line of code below mine does not use spaces inside the square brackets yet. Might need to clean that up as well? I actually copied that part for my patch.
#8
@
7 years ago
To me it seems having a notice/warning is better than failing silently. If you have a theme/plugin that is calling the_widget()
then you should know when it fails.
The other option would be to return a WP_Error
on failure, and true
on success.
#9
@
7 years ago
Agreed, I was about to suggest adding a _doing_it_wrong()
call here to tell developers that widgets need to be registered first
#10
@
7 years ago
I also agree, hence the reason my very first line in this ticket was:
First of all, this may be by design to notify the developer that they've probably made a mistake.
The reason I came across these notifications is my plugin was correctly registering and using, but another plugin (Yoast SEO) uses this call in certain situations:
<?php remove_all_actions( 'widgets_init' );
Which led to my widgets not being registered when I tried to use them and that's how I first encountered the undefined index notice. I'm only saying this to explain that sometimes this isn't a mistake by a developer per se, but could be because of another plugin's incorrect behavior.
I do agree a _doing_it_wrong()
is better than failing silently, so at least a developer will know something is wrong.
#11
@
7 years ago
- Owner set to obenland
- Resolution set to fixed
- Status changed from new to closed
In 41327:
#12
@
7 years ago
@obenland,
The text can be more informative. We can explain how to register widgets. It is done in other messages for developers.
Just replace:
_doing_it_wrong( __FUNCTION__, __( 'Widgets need to be registered before they can be displayed.' ), '4.9.0' );
With:
/* translators: %s: register_widget() */ _doing_it_wrong( __FUNCTION__, sprintf( __( 'Widgets need to be registered using %s, before they can be displayed.' ), '<code>register_widget()</code>' ), '4.9.0' );
This ticket was mentioned in Slack in #core by mrasharirfan. View the logs.
7 years ago
#14
@
7 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
Reopening to address comment:12.
Introduced in [11317].
Would you want to write a patch for that?