3 * Widget API: WP_Widget_Categories class
11 * Core class used to implement a Categories widget.
17 class WP_Widget_Categories extends WP_Widget {
20 * Sets up a new Categories widget instance.
25 public function __construct() {
27 'classname' => 'widget_categories',
28 'description' => __( 'A list or dropdown of categories.' ),
29 'customize_selective_refresh' => true,
31 parent::__construct( 'categories', __( 'Categories' ), $widget_ops );
35 * Outputs the content for the current Categories widget instance.
40 * @param array $args Display arguments including 'before_title', 'after_title',
41 * 'before_widget', and 'after_widget'.
42 * @param array $instance Settings for the current Categories widget instance.
44 public function widget( $args, $instance ) {
45 static $first_dropdown = true;
47 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
48 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base );
50 $c = ! empty( $instance['count'] ) ? '1' : '0';
51 $h = ! empty( $instance['hierarchical'] ) ? '1' : '0';
52 $d = ! empty( $instance['dropdown'] ) ? '1' : '0';
54 echo $args['before_widget'];
56 echo $args['before_title'] . $title . $args['after_title'];
66 $dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
67 $first_dropdown = false;
69 echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
71 $cat_args['show_option_none'] = __( 'Select Category' );
72 $cat_args['id'] = $dropdown_id;
75 * Filters the arguments for the Categories widget drop-down.
79 * @see wp_dropdown_categories()
81 * @param array $cat_args An array of Categories widget drop-down arguments.
83 wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args ) );
86 <script type='text/javascript'>
89 var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
90 function onCatChange() {
91 if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
92 location.href = "<?php echo home_url(); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value;
95 dropdown.onchange = onCatChange;
105 $cat_args['title_li'] = '';
108 * Filters the arguments for the Categories widget.
112 * @param array $cat_args An array of Categories widget options.
114 wp_list_categories( apply_filters( 'widget_categories_args', $cat_args ) );
120 echo $args['after_widget'];
124 * Handles updating settings for the current Categories widget instance.
129 * @param array $new_instance New settings for this instance as input by the user via
131 * @param array $old_instance Old settings for this instance.
132 * @return array Updated settings to save.
134 public function update( $new_instance, $old_instance ) {
135 $instance = $old_instance;
136 $instance['title'] = sanitize_text_field( $new_instance['title'] );
137 $instance['count'] = !empty($new_instance['count']) ? 1 : 0;
138 $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
139 $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
145 * Outputs the settings form for the Categories widget.
150 * @param array $instance Current settings.
152 public function form( $instance ) {
154 $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
155 $title = sanitize_text_field( $instance['title'] );
156 $count = isset($instance['count']) ? (bool) $instance['count'] :false;
157 $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
158 $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
160 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
161 <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>
163 <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
164 <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
166 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
167 <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br />
169 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
170 <label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>