Classic widgets with 5.9+?

This widget is built incorrectly.

There are 2 fundamental flaws here:

  • It is creating an object using new, which is incorrect. It is the responsibility of the Widgets API to create the object, not you, WP will create an instance of that class for each widget present, so if there are 5 HelloWidgets, then 5 objects will be created by WordPress.
  • Widget registration should never happen inside the widgets constructor. This is a mistake and an error.

Because of this misbehaviour the widget breaks on newer versions of WordPress, but more importantly it should never have worked to begin with, that it was able to run with code built this way is an accident/coincidence.

Fixing The Widget

First, remove the $hello_widget = new HelloWidget(); call, it is incorrect and not how widgets are intended to work.

Second, remove the register_widget call and hook from the constructor. It does not belong there. It needs to happen outside the class.

The final result should look similar to this:

/**
* A Hello Widget
*/
class HelloWidget extends WP_Widget {
    public function __construct() {
        parent::__construct(
            'hello-widget',
            __( 'Hello Widget', 'yourtextdomain' )
        );
    }

    ... rest of widgets functions
}

// Tell WordPress about our widget
add_action(
    'widgets_init',
    function() {
        register_widget( 'HelloWidget' );
    }
);

Also note that I added a textdomain to the __ call, and the registration has been moved outside of the widget.

Ideally the class should be in a file on its own, which is then included by the file that calls add_action. Defining things and doing things should not happen in the same file ( it makes it impossible to run tests in isolation and introduces tighter coupling which is undesirable and makes debugging harder ).