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() {
26 $widget_ops = array('classname' => 'widget_pages', 'description' => __( 'A list of your site’s Pages.') );
27 parent::__construct('pages', __('Pages'), $widget_ops);
31 * Outputs the content for the current Pages 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 Pages widget instance.
40 public function widget( $args, $instance ) {
43 * Filter the widget title.
47 * @param string $title The widget title. Default 'Pages'.
48 * @param array $instance An array of the widget's settings.
49 * @param mixed $id_base The widget ID.
51 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Pages' ) : $instance['title'], $instance, $this->id_base );
53 $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
54 $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
56 if ( $sortby == 'menu_order' )
57 $sortby = 'menu_order, post_title';
60 * Filter the arguments for the Pages widget.
64 * @see wp_list_pages()
66 * @param array $args An array of arguments to retrieve the pages list.
68 $out = wp_list_pages( apply_filters( 'widget_pages_args', array(
71 'sort_column' => $sortby,
75 if ( ! empty( $out ) ) {
76 echo $args['before_widget'];
78 echo $args['before_title'] . $title . $args['after_title'];
85 echo $args['after_widget'];
90 * Handles updating settings for the current Pages widget instance.
95 * @param array $new_instance New settings for this instance as input by the user via
97 * @param array $old_instance Old settings for this instance.
98 * @return array Updated settings to save.
100 public function update( $new_instance, $old_instance ) {
101 $instance = $old_instance;
102 $instance['title'] = sanitize_text_field( $new_instance['title'] );
103 if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
104 $instance['sortby'] = $new_instance['sortby'];
106 $instance['sortby'] = 'menu_order';
109 $instance['exclude'] = sanitize_text_field( $new_instance['exclude'] );
115 * Outputs the settings form for the Pages widget.
120 * @param array $instance Current settings.
122 public function form( $instance ) {
124 $instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') );
127 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label>
128 <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'] ); ?>" />
131 <label for="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>"><?php _e( 'Sort by:' ); ?></label>
132 <select name="<?php echo esc_attr( $this->get_field_name( 'sortby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>" class="widefat">
133 <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
134 <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
135 <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
139 <label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:' ); ?></label>
140 <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" />
142 <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>