[wp-trac] [WordPress Trac] #41743: Using the_widget on a widget that has not been registered results in an undefined index notice.

WordPress Trac noreply at wordpress.org
Mon Aug 28 13:24:37 UTC 2017


#41743: Using the_widget on a widget that has not been registered results in an
undefined index notice.
--------------------------+-----------------------------
 Reporter:  SeBsZ         |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Widgets       |    Version:  4.8.1
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 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:

 {{{#!php
 $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:

 {{{#!php
 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.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/41743>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list