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() {
26 $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your site.") );
27 parent::__construct( 'search', _x( 'Search', 'Search widget' ), $widget_ops );
31 * Outputs the content for the current Search 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 Search widget instance.
40 public function widget( $args, $instance ) {
41 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
42 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
44 echo $args['before_widget'];
46 echo $args['before_title'] . $title . $args['after_title'];
49 // Use current theme search form if it exists
52 echo $args['after_widget'];
56 * Outputs the settings form for the Search widget.
61 * @param array $instance Current settings.
63 public function form( $instance ) {
64 $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
65 $title = $instance['title'];
67 <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>
72 * Handles updating settings for the current Search widget instance.
77 * @param array $new_instance New settings for this instance as input by the user via
79 * @param array $old_instance Old settings for this instance.
80 * @return array Updated settings.
82 public function update( $new_instance, $old_instance ) {
83 $instance = $old_instance;
84 $new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
85 $instance['title'] = sanitize_text_field( $new_instance['title'] );