3 * Widget API: WP_Widget_Search class
11 * Core class used to implement a Search widget.
17 class WP_Widget_Search extends WP_Widget {
20 * Sets up a new Search widget instance.
25 public function __construct() {
27 'classname' => 'widget_search',
28 'description' => __( 'A search form for your site.' ),
29 'customize_selective_refresh' => true,
31 parent::__construct( 'search', _x( 'Search', 'Search widget' ), $widget_ops );
35 * Outputs the content for the current Search 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 Search widget instance.
44 public function widget( $args, $instance ) {
45 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
46 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
48 echo $args['before_widget'];
50 echo $args['before_title'] . $title . $args['after_title'];
53 // Use current theme search form if it exists
56 echo $args['after_widget'];
60 * Outputs the settings form for the Search widget.
65 * @param array $instance Current settings.
67 public function form( $instance ) {
68 $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
69 $title = $instance['title'];
71 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <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); ?>" /></label></p>
76 * Handles updating settings for the current Search widget instance.
81 * @param array $new_instance New settings for this instance as input by the user via
83 * @param array $old_instance Old settings for this instance.
84 * @return array Updated settings.
86 public function update( $new_instance, $old_instance ) {
87 $instance = $old_instance;
88 $new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
89 $instance['title'] = sanitize_text_field( $new_instance['title'] );