admin管理员组

文章数量:1126988

Recently updated to PHP8 and I'm getting a critical error related to the theme.

The critical error is:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function WP_Widget::__construct(), 0 passed in /www/scottsimonbooks_396/public/wp-includes/class-wp-widget-factory.php on line 62 and at least 2 expected in /www/scottsimonbooks_396/public/wp-includes/class-wp-widget.php:163 Stack trace: #0 /www/scottsimonbooks_396/public/wp-includes/class-wp-widget-factory.php(62): WP_Widget->__construct() #1 /www/scottsimonbooks_396/public/wp-includes/widgets.php(115): WP_Widget_Factory->register('thinker_recentp...') #2 /www/scottsimonbooks_396/public/wp-content/themes/thinker/inc/widgets.php(78): register_widget('thinker_recentp...') #3 /www/scottsimonbooks_396/public/wp-content/themes/thinker/functions.php(241): require('/www/scottsimon...') #4 /www/scottsimonbooks_396/public/wp-settings.php(585): include('/www/scottsimon...') #5 /www/scottsimonbooks_396/public/wp-config.php(87): require_once('/www/scottsimon...') #6 /www/scottsimonbooks_396/public/wp-load.php(50): require_once('/www/scottsimon...') #7 /www/scottsimonbooks_396/public/wp-blog-header.php(13): require_once('/www/scottsimon...') #8 /www/scottsimonbooks_396/public/index.php(17): require('/www/scottsimon...') #9 {main} thrown in /www/scottsimonbooks_396/public/wp-includes/class-wp-widget.php on line 163

I tried updating class-wp-widget-factory.php on line 63 by swapping:

$this->widgets[ $widget ] = new $widget();

for this $this->widgets[ $widget ] = new $widget( $widget, $widget ); but with no effect.

('thinker_recentp...') is a recent posts widget.

The code at line 163 in wp-includes/class-wp-widget.php is:

        if ( ! empty( $id_base ) ) {
            $id_base = strtolower( $id_base );
        } else {
            $id_base = preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) );
        }

        $this->id_base         = $id_base;
        $this->name            = $name;
        $this->option_name     = 'widget_' . $this->id_base;
        $this->widget_options  = wp_parse_args(
            $widget_options,
            array(
                'classname'                   => str_replace( '\\', '_', $this->option_name ),
                'customize_selective_refresh' => false,
            )
        );
        $this->control_options = wp_parse_args( $control_options, array( 'id_base' => $this->id_base ) );
    }```

Recently updated to PHP8 and I'm getting a critical error related to the theme.

The critical error is:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function WP_Widget::__construct(), 0 passed in /www/scottsimonbooks_396/public/wp-includes/class-wp-widget-factory.php on line 62 and at least 2 expected in /www/scottsimonbooks_396/public/wp-includes/class-wp-widget.php:163 Stack trace: #0 /www/scottsimonbooks_396/public/wp-includes/class-wp-widget-factory.php(62): WP_Widget->__construct() #1 /www/scottsimonbooks_396/public/wp-includes/widgets.php(115): WP_Widget_Factory->register('thinker_recentp...') #2 /www/scottsimonbooks_396/public/wp-content/themes/thinker/inc/widgets.php(78): register_widget('thinker_recentp...') #3 /www/scottsimonbooks_396/public/wp-content/themes/thinker/functions.php(241): require('/www/scottsimon...') #4 /www/scottsimonbooks_396/public/wp-settings.php(585): include('/www/scottsimon...') #5 /www/scottsimonbooks_396/public/wp-config.php(87): require_once('/www/scottsimon...') #6 /www/scottsimonbooks_396/public/wp-load.php(50): require_once('/www/scottsimon...') #7 /www/scottsimonbooks_396/public/wp-blog-header.php(13): require_once('/www/scottsimon...') #8 /www/scottsimonbooks_396/public/index.php(17): require('/www/scottsimon...') #9 {main} thrown in /www/scottsimonbooks_396/public/wp-includes/class-wp-widget.php on line 163

I tried updating class-wp-widget-factory.php on line 63 by swapping:

$this->widgets[ $widget ] = new $widget();

for this $this->widgets[ $widget ] = new $widget( $widget, $widget ); but with no effect.

('thinker_recentp...') is a recent posts widget.

The code at line 163 in wp-includes/class-wp-widget.php is:

        if ( ! empty( $id_base ) ) {
            $id_base = strtolower( $id_base );
        } else {
            $id_base = preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) );
        }

        $this->id_base         = $id_base;
        $this->name            = $name;
        $this->option_name     = 'widget_' . $this->id_base;
        $this->widget_options  = wp_parse_args(
            $widget_options,
            array(
                'classname'                   => str_replace( '\\', '_', $this->option_name ),
                'customize_selective_refresh' => false,
            )
        );
        $this->control_options = wp_parse_args( $control_options, array( 'id_base' => $this->id_base ) );
    }```
Share Improve this question edited Feb 24, 2023 at 10:29 Gavin asked Feb 24, 2023 at 10:01 GavinGavin 11 silver badge2 bronze badges 3
  • Don't edit core files. The error isn't in WordPress. It's in a theme or plugin. You can see from the error message that the error is related to a widget called 'thinker_recentp...', Do you know what that could be? – Jacob Peattie Commented Feb 24, 2023 at 10:11
  • It's a recent posts widget. It's in public/wp-content/themes/thinker/inc/widgets.php on the last line of the file is register_widget('thinker_recentposts'); – Gavin Commented Feb 24, 2023 at 10:25
  • Did you develop the theme? If not, your theme apparently has issues with PHP8 and you should check for an update or contact the author. – Jacob Peattie Commented Feb 24, 2023 at 10:34
Add a comment  | 

1 Answer 1

Reset to default 1

The error indicates that the WP_Widget::__construct() function expects at least two arguments to be passed but none were passed in the line of code where the error occurred. This could due to the upgrade to PHP 8 which is stricter about argument counts.

To fix the issue, you need to modify the code in the "recent posts" widget class in ( wp-content/themes/thinker/inc/widgets.php) to include the required arguments in the constructor function for the WP_Widget class. You need to modify the line that creates a new instance of the WP_Widget class like this:

Replace this code:

parent::__construct( 'thinker_recentposts', __( 'Thinker Recent Posts', 'thinker' ), $widget_ops );

with this code:

parent::__construct( 'thinker_recentposts', __( 'Thinker Recent Posts', 'thinker' ), array( 'description' => __( 'Displays recent posts with thumbnails', 'thinker' ) ) );

The difference is that we have added an array containing the "description" parameter, which is one of the required arguments for the WP_Widget constructor.

Once you have made this change, save the file and try reloading. The error message should no longer appear..

本文标签: theme developmentWordPress PHP8 Critical Error in classwpwidgetphp