WP_Widget_Pages

Core class used to implement a Pages widget.

Defined (1)

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

/wp-includes/widgets/class-wp-widget-pages.php  
  1. class WP_Widget_Pages extends WP_Widget { 
  2.  
  3. /** 
  4. * Sets up a new Pages widget instance. 
  5. * @since 2.8.0 
  6. * @access public 
  7. */ 
  8. public function __construct() { 
  9. $widget_ops = array( 
  10. 'classname' => 'widget_pages',  
  11. 'description' => __( 'A list of your site’s Pages.' ),  
  12. 'customize_selective_refresh' => true,  
  13. ); 
  14. parent::__construct( 'pages', __( 'Pages' ), $widget_ops ); 
  15.  
  16. /** 
  17. * Outputs the content for the current Pages 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 Pages widget instance. 
  23. */ 
  24. public function widget( $args, $instance ) { 
  25.  
  26. /** 
  27. * Filters the widget title. 
  28. * @since 2.6.0 
  29. * @param string $title The widget title. Default 'Pages'. 
  30. * @param array $instance An array of the widget's settings. 
  31. * @param mixed $id_base The widget ID. 
  32. */ 
  33. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Pages' ) : $instance['title'], $instance, $this->id_base ); 
  34.  
  35. $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby']; 
  36. $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude']; 
  37.  
  38. if ( $sortby == 'menu_order' ) 
  39. $sortby = 'menu_order, post_title'; 
  40.  
  41. /** 
  42. * Filters the arguments for the Pages widget. 
  43. * @since 2.8.0 
  44. * @see wp_list_pages() 
  45. * @param array $args An array of arguments to retrieve the pages list. 
  46. */ 
  47. $out = wp_list_pages( apply_filters( 'widget_pages_args', array( 
  48. 'title_li' => '',  
  49. 'echo' => 0,  
  50. 'sort_column' => $sortby,  
  51. 'exclude' => $exclude 
  52. ) ) ); 
  53.  
  54. if ( ! empty( $out ) ) { 
  55. echo $args['before_widget']; 
  56. if ( $title ) { 
  57. echo $args['before_title'] . $title . $args['after_title']; 
  58. ?> 
  59. <ul> 
  60. <?php echo $out; ?> 
  61. </ul> 
  62. <?php 
  63. echo $args['after_widget']; 
  64.  
  65. /** 
  66. * Handles updating settings for the current Pages widget instance. 
  67. * @since 2.8.0 
  68. * @access public 
  69. * @param array $new_instance New settings for this instance as input by the user via 
  70. * WP_Widget::form(). 
  71. * @param array $old_instance Old settings for this instance. 
  72. * @return array Updated settings to save. 
  73. */ 
  74. public function update( $new_instance, $old_instance ) { 
  75. $instance = $old_instance; 
  76. $instance['title'] = sanitize_text_field( $new_instance['title'] ); 
  77. if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) { 
  78. $instance['sortby'] = $new_instance['sortby']; 
  79. } else { 
  80. $instance['sortby'] = 'menu_order'; 
  81.  
  82. $instance['exclude'] = sanitize_text_field( $new_instance['exclude'] ); 
  83.  
  84. return $instance; 
  85.  
  86. /** 
  87. * Outputs the settings form for the Pages widget. 
  88. * @since 2.8.0 
  89. * @access public 
  90. * @param array $instance Current settings. 
  91. */ 
  92. public function form( $instance ) { 
  93. //Defaults 
  94. $instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') ); 
  95. ?> 
  96. <p> 
  97. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label> 
  98. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id('title') ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> 
  99. </p> 
  100. <p> 
  101. <label for="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>"><?php _e( 'Sort by:' ); ?></label> 
  102. <select name="<?php echo esc_attr( $this->get_field_name( 'sortby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>" class="widefat"> 
  103. <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option> 
  104. <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option> 
  105. <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option> 
  106. </select> 
  107. </p> 
  108. <p> 
  109. <label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:' ); ?></label> 
  110. <input type="text" value="<?php echo esc_attr( $instance['exclude'] ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'exclude' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>" class="widefat" /> 
  111. <br /> 
  112. <small><?php _e( 'Page IDs, separated by commas.' ); ?></small> 
  113. </p> 
  114. <?php 
  115.