adding WordPress custom widget multiple times overrides old values

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. 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.