Make WordPress Core

Opened 3 months ago

Closed 3 months ago

#60725 closed defect (bug) (duplicate)

class-wp-widget fatal error in PHP 8.2

Reported by: markhowe's profile markhowe Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Widgets Keywords:
Focuses: Cc:

Description (last modified by sabernhardt)

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function WP_Widget::__construct(), 0 passed in /.../wp-includes/class-wp-widget-factory.php on line 62 and at least 2 expected in /.../wp-includes/class-wp-widget.php:163

This error seems to have been identified previously, but without pointing to the file where the source of the problem lies (i.e. class-wp-widget-factory.php ). A simple Google search shows that a number of people who are using a theme that makes use of the wp-widget class have seen this fatal error when they have tried to upgrade to PHP 8.

Comparing the new $widget() call in class-wp-widget-factory.php with the constructor in class-wp-widget.php shows clearly that the constructor expects at least two arguments, but that class-wp-widget-factory.php supplies none. The fix that has been suggested (and which works on PHP 8.2) is the following:

public function register( $widget ) {
        if ( $widget instanceof WP_Widget ) {
            $this->widgets[ spl_object_hash( $widget ) ] = $widget;
        } else {
            // $this->widgets[ $widget ] = new $widget(); <---- the current line 62 ---------
            $this->widgets[ $widget ] = new $widget($widget, $widget); // fix for PHP 8
        }   
    }

Perhaps there is a more elegant solution, but this does work, and I have this version of the file running on a live server. Of course, each time I upgrade to the latest version of WordPress this file gets overwritten... So I would be very grateful if the current file could be made PHP 8 ready.

Change History (2)

#1 @sabernhardt
3 months ago

  • Component changed from General to Widgets
  • Description modified (diff)

#2 @sabernhardt
3 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Version 6.4.3 deleted

That error was already reported on #56127.

The StackExchange suggestion is only a temporary solution to get into the admin and replace the theme or deactivate a plugin that causes the problem. PHP 8+ is not particularly friendly to code that has not been updated for years.

A few support topics suggest ways to edit widget registration code within a theme or plugin. However, if your theme causes the error and you cannot update it a newer version, I think finding a new theme to replace it would be a better long-term strategy.

Note: See TracTickets for help on using tickets.