Extend widget class
The first step for creating your own custom widget is to extend the
WP Widget class.
WP_Widget class has 18 methods, but in this case, we will use only a few of them.
Inside the constructor, we will define base id, name and description of the widget.
Front-end display
Using a built-in method widget will handle the front-end display of the widget. Add widget function to our class. We want our widget to display the title which user provided and the names of categories which user selected. First, we filter the widget title and retrieve all selected categories. The last thing here is to display them.
Back-end form
Check the instance for the title and selected categories otherwise set defaults. With functions get_field_id, get_field_name, we are setting attributes for fields to be saved by the update method. We retrieve categories with get_categories(). Category name is compared with selected categories in the instance, so we can add checked status to checkbox if user selected this category.
Update
Return instance created from new user inputs.