1. 首页
  2. WordPress 函数手册

get_search_form()

get_search_form( array $args = array() )

显示搜索表单。
Display search form.

目录锚点:#说明#返回#源码#笔记


说明(Description)

将首先尝试查找搜索表单.php在子对象或父对象中创建文件,然后加载它。如果不存在,则将显示默认搜索表单。默认的搜索表单是HTML,它将被显示。有一个应用于搜索表单HTML的筛选器,用于编辑或替换它。过滤器是“获取搜索表单”。

此功能主要用于希望将搜索表单硬编码到侧栏中的主题,也可用于WordPress中的搜索小部件。

当函数运行时,还会调用一个名为“pre-get-search-form”的操作。这对于输出搜索所依赖的JavaScript或应用于搜索开头的各种格式非常有用。举例说明它的用途。


返回(Return)

(void|string)void如果“echo”参数为true,则搜索表单HTML如果“echo”为false。


源码(Source)

/**
 * Display search form.
 *
 * Will first attempt to locate the searchform.php file in either the child or
 * the parent, then load it. If it doesn't exist, then the default search form
 * will be displayed. The default search form is HTML, which will be displayed.
 * There is a filter applied to the search form HTML in order to edit or replace
 * it. The filter is 'get_search_form'.
 *
 * This function is primarily used by themes which want to hardcode the search
 * form into the sidebar and also by the search widget in WordPress.
 *
 * There is also an action that is called whenever the function is run called,
 * 'pre_get_search_form'. This can be useful for outputting JavaScript that the
 * search relies on or various formatting that applies to the beginning of the
 * search. To give a few examples of what it can be used for.
 *
 * @since 2.7.0
 *
 * @param bool $echo Default to echo and not return the form.
 * @return string|void String when $echo is false.
 */
function get_search_form( $echo = true ) {
	/**
	 * Fires before the search form is retrieved, at the start of get_search_form().
	 *
	 * @since 2.7.0 as 'get_search_form' action.
	 * @since 3.6.0
	 *
	 * @link https://core.trac.wordpress.org/ticket/19321
	 */
	do_action( 'pre_get_search_form' );

	$format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml';

	/**
	 * Filter the HTML format of the search form.
	 *
	 * @since 3.6.0
	 *
	 * @param string $format The type of markup to use in the search form.
	 *                       Accepts 'html5', 'xhtml'.
	 */
	$format = apply_filters( 'search_form_format', $format );

	$search_form_template = locate_template( 'searchform.php' );
	if ( '' != $search_form_template ) {
		ob_start();
		require( $search_form_template );
		$form = ob_get_clean();
	} else {
		if ( 'html5' == $format ) {
			$form = '
				
					' . _x( 'Search for:', 'label' ) . '
					
				
				
			';
		} else {
			$form = '
				
					' . _x( 'Search for:', 'label' ) . '
					
					
				
			';
		}
	}

	/**
	 * Filter the HTML output of the search form.
	 *
	 * @since 2.7.0
	 *
	 * @param string $form The search form HTML output.
	 */
	$result = apply_filters( 'get_search_form', $form );

	if ( null === $result )
		$result = $form;

	if ( $echo )
		echo $result;
	else
		return $result;
}
更新版本 源码位置 使用 被使用
5.2.0 wp-includes/general-template.php:201 1 function 15

笔记(Notes)

主题形式
默认HTML5表单
默认HTML4表单

get_search_form() 为WP2原创文章,链接:https://www.wp2.cn/functions/get_search_form/

发表评论

您的电子邮箱地址不会被公开。