[wp-trac] [WordPress Trac] #56127: WP_Widget constructor throws fatal ArgumentCountError

WordPress Trac noreply at wordpress.org
Mon Aug 7 20:31:38 UTC 2023


#56127: WP_Widget constructor throws fatal ArgumentCountError
-------------------------------------------+-------------------------------
 Reporter:  firefly2000                    |       Owner:  (none)
     Type:  defect (bug)                   |      Status:  new
 Priority:  normal                         |   Milestone:  Awaiting Review
Component:  Widgets                        |     Version:  6.0
 Severity:  normal                         |  Resolution:
 Keywords:  needs-patch reporter-feedback  |     Focuses:  coding-standards
-------------------------------------------+-------------------------------

Comment (by hellofromTonya):

 As @TimothyBlynJacobs noted, widgets should extend `WP_Widget` and declare
 a constructor with no parameters that invokes the `WP_Widget::__construct`
 with the required arguments as shown in the documentation
 https://developer.wordpress.org/themes/functionality/widgets/. For
 example:

 {{{#!php
 <?php
 class My_Widget extends WP_Widget {
         public function __construct() {
                 parent::__construct(
                         'my-text',  // Base ID
                         'My Text'   // Name
                 );
 }}}

 The code in `WP_Widget_Factory::register( $widget )` that instantiates
 `new $widget()` expects and requires no parameters being defined in the
 widget. This means themes and plugins are required to follow the above
 code.

 This code has been in Core since WP 2.8.0.

 * [10782] added `WP_Widget` which also had a similar constructor as now.
 * [10808] introduced the `WP_Widget_Factory` whose `register` method
 accepted a string of the widget class name and instantiated as `new
 $widget_class()`.

 Notice that the code flagged in the ticket's description was introduced in
 WP 2.8.0. The design then and now is as @TimothyBlynJacobs noted above,
 i.e. widgets should extend `WP_Widget` with no parameters in the
 constructor.

 I'm not sure why this issue is flagged as occurring in WP 6.0.0 release.
 But to double check, next I'll scanning Core's source code for all widget
 classes to ensure each is properly doing their constructor.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56127#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list