WP_Widget_Categories

Core class used to implement a Categories widget.

Defined (1)

The class is defined in the following location(s).

/wp-includes/widgets/class-wp-widget-categories.php  
  1. class WP_Widget_Categories extends WP_Widget { 
  2.  
  3. /** 
  4. * Sets up a new Categories widget instance. 
  5. * @since 2.8.0 
  6. * @access public 
  7. */ 
  8. public function __construct() { 
  9. $widget_ops = array( 
  10. 'classname' => 'widget_categories',  
  11. 'description' => __( 'A list or dropdown of categories.' ),  
  12. 'customize_selective_refresh' => true,  
  13. ); 
  14. parent::__construct( 'categories', __( 'Categories' ), $widget_ops ); 
  15.  
  16. /** 
  17. * Outputs the content for the current Categories widget instance. 
  18. * @since 2.8.0 
  19. * @access public 
  20. * @param array $args Display arguments including 'before_title', 'after_title',  
  21. * 'before_widget', and 'after_widget'. 
  22. * @param array $instance Settings for the current Categories widget instance. 
  23. */ 
  24. public function widget( $args, $instance ) { 
  25. static $first_dropdown = true; 
  26.  
  27. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 
  28. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base ); 
  29.  
  30. $c = ! empty( $instance['count'] ) ? '1' : '0'; 
  31. $h = ! empty( $instance['hierarchical'] ) ? '1' : '0'; 
  32. $d = ! empty( $instance['dropdown'] ) ? '1' : '0'; 
  33.  
  34. echo $args['before_widget']; 
  35. if ( $title ) { 
  36. echo $args['before_title'] . $title . $args['after_title']; 
  37.  
  38. $cat_args = array( 
  39. 'orderby' => 'name',  
  40. 'show_count' => $c,  
  41. 'hierarchical' => $h 
  42. ); 
  43.  
  44. if ( $d ) { 
  45. $dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}"; 
  46. $first_dropdown = false; 
  47.  
  48. echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>'; 
  49.  
  50. $cat_args['show_option_none'] = __( 'Select Category' ); 
  51. $cat_args['id'] = $dropdown_id; 
  52.  
  53. /** 
  54. * Filters the arguments for the Categories widget drop-down. 
  55. * @since 2.8.0 
  56. * @see wp_dropdown_categories() 
  57. * @param array $cat_args An array of Categories widget drop-down arguments. 
  58. */ 
  59. wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args ) ); 
  60. ?> 
  61.  
  62. <script type='text/javascript'> 
  63. /* <![CDATA[ */ 
  64. (function() { 
  65. var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" ); 
  66. function onCatChange() { 
  67. if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) { 
  68. location.href = "<?php echo home_url(); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value; 
  69. dropdown.onchange = onCatChange; 
  70. })(); 
  71. /* ]]> */ 
  72. </script> 
  73.  
  74. <?php 
  75. } else { 
  76. ?> 
  77. <ul> 
  78. <?php 
  79. $cat_args['title_li'] = ''; 
  80.  
  81. /** 
  82. * Filters the arguments for the Categories widget. 
  83. * @since 2.8.0 
  84. * @param array $cat_args An array of Categories widget options. 
  85. */ 
  86. wp_list_categories( apply_filters( 'widget_categories_args', $cat_args ) ); 
  87. ?> 
  88. </ul> 
  89. <?php 
  90.  
  91. echo $args['after_widget']; 
  92.  
  93. /** 
  94. * Handles updating settings for the current Categories widget instance. 
  95. * @since 2.8.0 
  96. * @access public 
  97. * @param array $new_instance New settings for this instance as input by the user via 
  98. * WP_Widget::form(). 
  99. * @param array $old_instance Old settings for this instance. 
  100. * @return array Updated settings to save. 
  101. */ 
  102. public function update( $new_instance, $old_instance ) { 
  103. $instance = $old_instance; 
  104. $instance['title'] = sanitize_text_field( $new_instance['title'] ); 
  105. $instance['count'] = !empty($new_instance['count']) ? 1 : 0; 
  106. $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0; 
  107. $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0; 
  108.  
  109. return $instance; 
  110.  
  111. /** 
  112. * Outputs the settings form for the Categories widget. 
  113. * @since 2.8.0 
  114. * @access public 
  115. * @param array $instance Current settings. 
  116. */ 
  117. public function form( $instance ) { 
  118. //Defaults 
  119. $instance = wp_parse_args( (array) $instance, array( 'title' => '') ); 
  120. $title = sanitize_text_field( $instance['title'] ); 
  121. $count = isset($instance['count']) ? (bool) $instance['count'] :false; 
  122. $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false; 
  123. $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false; 
  124. ?> 
  125. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label> 
  126. <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> 
  127.  
  128. <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 ); ?> /> 
  129. <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br /> 
  130.  
  131. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> /> 
  132. <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br /> 
  133.  
  134. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> /> 
  135. <label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p> 
  136. <?php 
  137.