#57739 closed defect (bug) (duplicate)
Fatal error in WP_Widget_Factory::register() on PHP 8.1
Reported by: | bcm924 | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | major | Version: | |
Component: | Widgets | Keywords: | has-patch php81 |
Focuses: | Cc: |
Description
In our case, our website (latest WP release) was throwing fatal errors when trying to switch to PHP 8.1. After much digging, we found the issue to be in the wp-includes/class-wp-widget-factory.php file, line 62. The line was formerly:
<?php $this->widgets[ $widget ] = new $widget();
And to fix the site, we modified the line to be as follows:
<?php $this->widgets[ $widget ] = new $widget( $widget, $widget );
This completely resolved the issue in our case. Please update the core file to reflect this change in the move toward PHP 8+ compatibility. Thanks.
Change History (10)
This ticket was mentioned in PR #4088 on WordPress/wordpress-develop by @codemonksuvro.
2 years ago
#1
- Keywords has-patch added
#4
in reply to:
↑ 2
;
follow-up:
↓ 5
@
2 years ago
Replying to codemonksuvro:
@codemonksuvro it works! Thx
Please check and let me know if it works or not. @bcm924
#5
in reply to:
↑ 4
;
follow-up:
↓ 6
@
2 years ago
@bcm924 It's nice that you have created this ticket. Many many thanks. Have a great day.
#7
@
22 months ago
- Keywords php81 added
- Milestone changed from Awaiting Review to 6.3
- Summary changed from PHP 8.1 compatibility to Fatal error in WP_Widget_Factory::register() on PHP 8.1
#8
@
22 months ago
Thanks @bcm924 for opening this ticket to discuss this issue.
I'm not so sure this is the correct fix.
The function signature of the __construct()
method of each widget can be quite different as each widget can implement this method differently and does not necessarily have to have declared the same number of required arguments as the parent class.
The __construct()
method is exempt from the normal method signature inheritance rules.
I do believe that you correctly pinpoint an underlying architectural/code design issue here with the wdiget factory class making presumptions about the method signature, but applying this "plaster" rather hides that issue instead of solving it and could well result in new errors being thrown for other widget implementations.
To solve your problem, I think more digging is needed to figure out which individual widget class is causing the error and for that widget class to be updated to be compatible with PHP 8.1.
A backtrace may help.
#9
@
21 months ago
- Milestone 6.3 deleted
- Resolution set to duplicate
- Status changed from new to closed
This appears to be a duplicate of #56127, let's continue there.
@SergeyBiryukov commented on PR #4088:
21 months ago
#10
Thanks for the PR! Closing for now as per https://core.trac.wordpress.org/ticket/57739#comment:8.
PHP 8.1 compatibility
wp-includes/class-wp-widget-factory.php file, line 62.
The line was formerly: $this->widgets[ $widget ] = new $widget();
I modified the line to be as follows: $this->widgets[ $widget ] = new $widget( $widget, $widget );
Trac ticket: https://core.trac.wordpress.org/ticket/57739