WP_Widget_Search
云策文档标注
概述
WP_Widget_Search 是 WordPress 核心类,用于实现搜索小部件。它继承自 WP_Widget,提供搜索表单的显示和设置功能。
关键要点
- WP_Widget_Search 继承自 WP_Widget,用于创建和管理搜索小部件。
- 主要方法包括 __construct(构造函数)、widget(输出小部件内容)、form(输出设置表单)和 update(处理设置更新)。
- 支持自定义刷新和 REST API 实例显示,通过 get_search_form() 调用活动主题的搜索表单。
代码示例
class WP_Widget_Search extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'widget_search',
'description' => __( 'A search form for your site.' ),
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
);
parent::__construct( 'search', _x( 'Search', 'Search widget' ), $widget_ops );
}
public function widget( $args, $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
get_search_form();
echo $args['after_widget'];
}
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = $instance['title'];
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<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 ); ?>" /></p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
return $instance;
}
}注意事项
- 使用 get_search_form() 确保与活动主题的搜索表单兼容。
- 设置更新时通过 sanitize_text_field 进行清理,增强安全性。
- 自 WordPress 2.8.0 版本引入,相关基类为 WP_Widget。
原文内容
Core class used to implement a Search widget.
Description
See also
Methods
| Name | Description |
|---|---|
| WP_Widget_Search::__construct | Sets up a new Search widget instance. |
| WP_Widget_Search::form | Outputs the settings form for the Search widget. |
| WP_Widget_Search::update | Handles updating settings for the current Search widget instance. |
| WP_Widget_Search::widget | Outputs the content for the current Search widget instance. |
Source
class WP_Widget_Search extends WP_Widget {
/**
* Sets up a new Search widget instance.
*
* @since 2.8.0
*/
public function __construct() {
$widget_ops = array(
'classname' => 'widget_search',
'description' => __( 'A search form for your site.' ),
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
);
parent::__construct( 'search', _x( 'Search', 'Search widget' ), $widget_ops );
}
/**
* Outputs the content for the current Search widget instance.
*
* @since 2.8.0
*
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance Settings for the current Search widget instance.
*/
public function widget( $args, $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
// Use active theme search form if it exists.
get_search_form();
echo $args['after_widget'];
}
/**
* Outputs the settings form for the Search widget.
*
* @since 2.8.0
*
* @param array $instance Current settings.
*/
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = $instance['title'];
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"></label>
<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 ); ?>" />
</p>
'' ) );
$instance['title'] = sanitize_text_field( $new_instance['title'] );
return $instance;
}
}
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |