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.net/current/web/wp/wp-includes/class-wp-widget-factory.php on line 61 and at least 2 expected
What am I doing wrong? Thanks!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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' );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0