最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

widgets - Too few arguments to function WP_Widget::__construct(),

programmeradmin1浏览0评论

first time with custom widget. I'm trying to create it this way:

<?php
namespace App;

class My_Widget extends \WP_Widget {
    function My_Widget() {

        $widget_ops = [
            'name' => 'My widget',
            'description' => 'My description'
        ];
        parent::__construct( 'My_Widget', 'My widget', $widget_ops );
    }
}

function load_widget() {
    register_widget( 'App\\my_widget' );
}
add_action( 'widgets_init', __NAMESPACE__ . '\\load_widget' );

And I get this error:

Too few arguments to function WP_Widget::__construct(), 0 passed in /srv/www/contraindicaciones/current/web/wp/wp-includes/class-wp-widget-factory.php on line 61 and at least 2 expected

What am I doing wrong? Thanks!

first time with custom widget. I'm trying to create it this way:

<?php
namespace App;

class My_Widget extends \WP_Widget {
    function My_Widget() {

        $widget_ops = [
            'name' => 'My widget',
            'description' => 'My description'
        ];
        parent::__construct( 'My_Widget', 'My widget', $widget_ops );
    }
}

function load_widget() {
    register_widget( 'App\\my_widget' );
}
add_action( 'widgets_init', __NAMESPACE__ . '\\load_widget' );

And I get this error:

Too few arguments to function WP_Widget::__construct(), 0 passed in /srv/www/contraindicaciones/current/web/wp/wp-includes/class-wp-widget-factory.php on line 61 and at least 2 expected

What am I doing wrong? Thanks!

Share Improve this question asked Feb 21, 2021 at 14:05 aitoraitor 7252 gold badges8 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 12

You're using an extremely outdated method of creating a widget. You should be using the __construct() function, not a named function, as the constructor, as documented.

namespace App;

class My_Widget extends \WP_Widget {
    function __construct() {

        $widget_ops = [
            'name' => 'My widget',
            'description' => 'My description'
        ];
        parent::__construct( 'My_Widget', 'My widget', $widget_ops );
    }
}

function load_widget() {
    register_widget( 'App\\my_widget' );
}
add_action( 'widgets_init', __NAMESPACE__ . '\\load_widget' );
发布评论

评论列表(0)

  1. 暂无评论