3 * Widget API: WP_Widget_Pages class
11 * Core class used to implement a Pages widget.
17 class WP_Widget_Pages extends WP_Widget {
20 * Sets up a new Pages widget instance.
25 public function __construct() {
27 'classname' => 'widget_pages',
28 'description' => __( 'A list of your site’s Pages.' ),
29 'customize_selective_refresh' => true,
31 parent::__construct( 'pages', __( 'Pages' ), $widget_ops );
35 * Outputs the content for the current Pages 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 Pages widget instance.
44 public function widget( $args, $instance ) {
47 * Filters the widget title.
51 * @param string $title The widget title. Default 'Pages'.
52 * @param array $instance An array of the widget's settings.
53 * @param mixed $id_base The widget ID.
55 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Pages' ) : $instance['title'], $instance, $this->id_base );
57 $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
58 $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
60 if ( $sortby == 'menu_order' )
61 $sortby = 'menu_order, post_title';
64 * Filters the arguments for the Pages widget.
68 * @see wp_list_pages()
70 * @param array $args An array of arguments to retrieve the pages list.
72 $out = wp_list_pages( apply_filters( 'widget_pages_args', array(
75 'sort_column' => $sortby,
79 if ( ! empty( $out ) ) {
80 echo $args['before_widget'];
82 echo $args['before_title'] . $title . $args['after_title'];
89 echo $args['after_widget'];
94 * Handles updating settings for the current Pages widget instance.
99 * @param array $new_instance New settings for this instance as input by the user via
101 * @param array $old_instance Old settings for this instance.
102 * @return array Updated settings to save.
104 public function update( $new_instance, $old_instance ) {
105 $instance = $old_instance;
106 $instance['title'] = sanitize_text_field( $new_instance['title'] );
107 if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
108 $instance['sortby'] = $new_instance['sortby'];
110 $instance['sortby'] = 'menu_order';
113 $instance['exclude'] = sanitize_text_field( $new_instance['exclude'] );
119 * Outputs the settings form for the Pages widget.
124 * @param array $instance Current settings.
126 public function form( $instance ) {
128 $instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') );
131 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label>
132 <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'] ); ?>" />
135 <label for="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>"><?php _e( 'Sort by:' ); ?></label>
136 <select name="<?php echo esc_attr( $this->get_field_name( 'sortby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>" class="widefat">
137 <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
138 <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
139 <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
143 <label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:' ); ?></label>
144 <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" />
146 <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>