3 * Widget API: WP_Widget_Archives class
11 * Core class used to implement the Archives widget.
17 class WP_Widget_Archives extends WP_Widget {
20 * Sets up a new Archives widget instance.
25 public function __construct() {
26 $widget_ops = array('classname' => 'widget_archive', 'description' => __( 'A monthly archive of your site’s Posts.') );
27 parent::__construct('archives', __('Archives'), $widget_ops);
31 * Outputs the content for the current Archives widget instance.
36 * @param array $args Display arguments including 'before_title', 'after_title',
37 * 'before_widget', and 'after_widget'.
38 * @param array $instance Settings for the current Archives widget instance.
40 public function widget( $args, $instance ) {
41 $c = ! empty( $instance['count'] ) ? '1' : '0';
42 $d = ! empty( $instance['dropdown'] ) ? '1' : '0';
44 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
45 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Archives' ) : $instance['title'], $instance, $this->id_base );
47 echo $args['before_widget'];
49 echo $args['before_title'] . $title . $args['after_title'];
53 $dropdown_id = "{$this->id_base}-dropdown-{$this->number}";
55 <label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo $title; ?></label>
56 <select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
59 * Filter the arguments for the Archives widget drop-down.
63 * @see wp_get_archives()
65 * @param array $args An array of Archives widget drop-down arguments.
67 $dropdown_args = apply_filters( 'widget_archives_dropdown_args', array(
70 'show_post_count' => $c
73 switch ( $dropdown_args['type'] ) {
75 $label = __( 'Select Year' );
78 $label = __( 'Select Month' );
81 $label = __( 'Select Day' );
84 $label = __( 'Select Week' );
87 $label = __( 'Select Post' );
92 <option value=""><?php echo esc_attr( $label ); ?></option>
93 <?php wp_get_archives( $dropdown_args ); ?>
100 * Filter the arguments for the Archives widget.
104 * @see wp_get_archives()
106 * @param array $args An array of Archives option arguments.
108 wp_get_archives( apply_filters( 'widget_archives_args', array(
110 'show_post_count' => $c
117 echo $args['after_widget'];
121 * Handles updating settings for the current Archives widget instance.
126 * @param array $new_instance New settings for this instance as input by the user via
127 * WP_Widget_Archives::form().
128 * @param array $old_instance Old settings for this instance.
129 * @return array Updated settings to save.
131 public function update( $new_instance, $old_instance ) {
132 $instance = $old_instance;
133 $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
134 $instance['title'] = sanitize_text_field( $new_instance['title'] );
135 $instance['count'] = $new_instance['count'] ? 1 : 0;
136 $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
142 * Outputs the settings form for the Archives widget.
147 * @param array $instance Current settings.
149 public function form( $instance ) {
150 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
151 $title = sanitize_text_field( $instance['title'] );
153 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
155 <input class="checkbox" type="checkbox"<?php checked( $instance['dropdown'] ); ?> id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>" /> <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('Display as dropdown'); ?></label>
157 <input class="checkbox" type="checkbox"<?php checked( $instance['count'] ); ?> id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" /> <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label>