Building collapsible plugin

$ WP_Widget_Archives = new WP_Widget_Archives ();
// use class methods

Methods

__construct ()
Installs an instance of the widget.
form ($ instance)
Displays a form with the input of the widget settings (in the admin panel).
update ($ new_instance, $ old_instance)
Controls updating of the widget settings.
widget ($ args, $ instance)
Displays the content of the widget (in the front of the site).

The content that the Archives widget generates on the front of the site is generated using the wp_get_archives () function. From this it follows that all the parameters that this function accepts can be passed on the widget_archives_dropdown_args and widget_archives_args filter hooks for customization.
Examples of

1 WooCommerce Product Archives Widget

If you do not need the default Archives widget, which by default displays post archives, you can use hook filters to display archives of WooCommerce products. The appearance will be the same as for the posts, only when the month is selected (by default), the user will go to the archive of products published this month.

add_filter (‘widget_archives_dropdown_args’, ‘wc_widget_archives_args’); // For a dropdown list
add_filter (‘widget_archives_args’, ‘wc_widget_archives_args’); // For bulleted list

function wc_widget_archives_args ($ args) {
$ args [‘post_type’] = ‘product’;

return $ args;
}

2 Archives widget by day

If records are often published on the site, then you can display the archives by day and limit the number of displayed days.

add_filter (‘widget_archives_dropdown_args’, ‘widget_archives_days’);

function wc_add_widget_archives_dropdown_args ($ args) {
$ args [‘type’] = ‘daily’;
$ args [‘limit’] = 5;

return $ args;
}

3 create an improved archives widget

In the front part of the site, a drop-down or bulleted list is formed using the wp_get_archives () function, which can take more parameters than can be specified in the standard Archives widget. Let’s create our own “Improved archives” widget based on the “Archives” widget, where we will provide a choice:

what type of archive (by years, by months, by weeks, and so on);
how many links to display;
create links to archives of what type of recording.

‘monthly’, ‘limit’ => ”, ‘post_type’ => ‘post’,]; public function __construct () { $ widget_ops = [ ‘description’ => ‘Improved archive widget.’, ]; WP_Widget :: __ construct (‘archives_advanced’, ‘Improved archives’, $ widget_ops); } // html form of widget settings in the Admin panel public function form ($ instance) { parent :: form ($ instance); $ instance = wp_parse_args ((array) $ instance, $ this-> default); $ options = [ ‘monthly’ => ‘By months’, ‘yearly’ => ‘Yearly’, ‘daily’ => ‘By day’, ‘weekly’ => ‘By week’, ‘postbypost’ => ‘By posts (sorted by date)’, ‘alpha’ => ‘By posts (sorted by title)’, ]; ?>

Archive type $ text):?> >

Number of links to archives Leave blank to display all links

Post type true,], ‘objects’) as $ post_type):?> name); ?> > label); ?>

<? php
}

// Save widget settings
public function update ($ new_instance, $ old_instance) {
$ new_instance = wp_parse_args ((array) $ new_instance, $ this-> default);

$ instance = parent :: update ($ new_instance, $ old_instance);
$ instance [‘type’] = sanitize_key ($ new_instance [‘type’]);