adding Wordpress custom widget multiple times overrides old values


June 03, 2018 at 16:41
Wordpress

A custom wordpress widget wouldn't work as soon as I inserted two or more of the widgets. Saving a value wrote them to all the widgets. The reason seems to be that I forgot to set a base ID in the parent constructor: 

class CustomWidget extends WP_Widget {
    function __construct() {

        $widget_ops = array(
            'classname' => 'ophi_custom_widget',
            'description' => 'Ophi Custom Widget',
        );

        // Instantiate the parent object
        $baseId = 'ophi-custom-widget';
        parent::__construct( $baseId, 'a custom Widget', $widget_ops );
    }

}

See line 11 where baseId is set. Before this I had set it to false which resulted in the error. 


Tags: register_widget custom widget


Hinterlasse einen Kommentar:
Search