get_search_form

函式
get_search_form ( $args = array() )
引數
  • (array) $args { Optional. Array of display arguments. @type bool $echo Whether to echo or return the form. Default true. @type string $aria_label ARIA label for the search form. Useful to distinguish multiple search forms on the same page and improve accessibility. Default empty. }
    Required:
    Default: array()
返回值
  • (void|string) Void if 'echo' argument is true, search form HTML if 'echo' is false.
定義位置
相關方法
get_search_queryinstall_search_formget_search_linkget_search_templateget_search_feed_link
引入
2.7.0
棄用
-

get_search_form函式是一個WordPress函式,用於檢索搜尋表單的HTML標記: 這個函式接受一個可選的表單屬性引數,並返回搜尋表單的HTML標記。

顯示搜尋表單。

將首先嚐試在子目錄或父目錄中找到 searchform.php 檔案,然後載入它。如果它不存在,那麼預設的搜尋表單將被顯示。預設的搜尋表單是HTML,它將被顯示。有一個過濾器應用於搜尋表單的HTML,以便編輯或替換它。該過濾器是{@see ‘get_search_form’}。

這個函式主要被那些想把搜尋表單硬編碼到側邊欄的主題使用,也被WordPress中的搜尋小工具使用。

還有一個動作,在該函式執行時被呼叫,{@see ‘pre_get_search_form’}。這對於輸出搜尋所依賴的JavaScript或適用於搜尋開始的各種格式化是很有用的。舉幾個例子說明它可以用來做什麼。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_search_form( $args = array() ) {
/**
* 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
* @since 5.5.0 The `$args` parameter was added.
*
* @link https://core.trac.wordpress.org/ticket/19321
*
* @param array $args The array of arguments for building the search form.
* See get_search_form() for information on accepted arguments.
*/
do_action( 'pre_get_search_form', $args );
$echo = true;
if ( ! is_array( $args ) ) {
/*
* Back compat: to ensure previous uses of get_search_form() continue to
* function as expected, we handle a value for the boolean $echo param removed
* in 5.2.0. Then we deal with the $args array and cast its defaults.
*/
$echo = (bool) $args;
// Set an empty array and allow default arguments to take over.
$args = array();
}
// Defaults are to echo and to output no custom label on the form.
$defaults = array(
'echo' => $echo,
'aria_label' => '',
);
$args = wp_parse_args( $args, $defaults );
/**
* Filters the array of arguments used when generating the search form.
*
* @since 5.2.0
*
* @param array $args The array of arguments for building the search form.
* See get_search_form() for information on accepted arguments.
*/
$args = apply_filters( 'search_form_args', $args );
// Ensure that the filtered arguments contain all required default values.
$args = array_merge( $defaults, $args );
$format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml';
/**
* Filters the HTML format of the search form.
*
* @since 3.6.0
* @since 5.5.0 The `$args` parameter was added.
*
* @param string $format The type of markup to use in the search form.
* Accepts 'html5', 'xhtml'.
* @param array $args The array of arguments for building the search form.
* See get_search_form() for information on accepted arguments.
*/
$format = apply_filters( 'search_form_format', $format, $args );
$search_form_template = locate_template( 'searchform.php' );
if ( '' !== $search_form_template ) {
ob_start();
require $search_form_template;
$form = ob_get_clean();
} else {
// Build a string containing an aria-label to use for the search form.
if ( $args['aria_label'] ) {
$aria_label = 'aria-label="' . esc_attr( $args['aria_label'] ) . '" ';
} else {
/*
* If there's no custom aria-label, we can set a default here. At the
* moment it's empty as there's uncertainty about what the default should be.
*/
$aria_label = '';
}
if ( 'html5' === $format ) {
$form = '<form role="search" ' . $aria_label . 'method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
<label>
<span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span>
<input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search &hellip;', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" />
</label>
<input type="submit" class="search-submit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
</form>';
} else {
$form = '<form role="search" ' . $aria_label . 'method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
<div>
<label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
<input type="text" value="' . get_search_query() . '" name="s" id="s" />
<input type="submit" id="searchsubmit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
</div>
</form>';
}
}
/**
* Filters the HTML output of the search form.
*
* @since 2.7.0
* @since 5.5.0 The `$args` parameter was added.
*
* @param string $form The search form HTML output.
* @param array $args The array of arguments for building the search form.
* See get_search_form() for information on accepted arguments.
*/
$result = apply_filters( 'get_search_form', $form, $args );
if ( null === $result ) {
$result = $form;
}
if ( $args['echo'] ) {
echo $result;
} else {
return $result;
}
}
function get_search_form( $args = array() ) { /** * 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 * @since 5.5.0 The `$args` parameter was added. * * @link https://core.trac.wordpress.org/ticket/19321 * * @param array $args The array of arguments for building the search form. * See get_search_form() for information on accepted arguments. */ do_action( 'pre_get_search_form', $args ); $echo = true; if ( ! is_array( $args ) ) { /* * Back compat: to ensure previous uses of get_search_form() continue to * function as expected, we handle a value for the boolean $echo param removed * in 5.2.0. Then we deal with the $args array and cast its defaults. */ $echo = (bool) $args; // Set an empty array and allow default arguments to take over. $args = array(); } // Defaults are to echo and to output no custom label on the form. $defaults = array( 'echo' => $echo, 'aria_label' => '', ); $args = wp_parse_args( $args, $defaults ); /** * Filters the array of arguments used when generating the search form. * * @since 5.2.0 * * @param array $args The array of arguments for building the search form. * See get_search_form() for information on accepted arguments. */ $args = apply_filters( 'search_form_args', $args ); // Ensure that the filtered arguments contain all required default values. $args = array_merge( $defaults, $args ); $format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml'; /** * Filters the HTML format of the search form. * * @since 3.6.0 * @since 5.5.0 The `$args` parameter was added. * * @param string $format The type of markup to use in the search form. * Accepts 'html5', 'xhtml'. * @param array $args The array of arguments for building the search form. * See get_search_form() for information on accepted arguments. */ $format = apply_filters( 'search_form_format', $format, $args ); $search_form_template = locate_template( 'searchform.php' ); if ( '' !== $search_form_template ) { ob_start(); require $search_form_template; $form = ob_get_clean(); } else { // Build a string containing an aria-label to use for the search form. if ( $args['aria_label'] ) { $aria_label = 'aria-label="' . esc_attr( $args['aria_label'] ) . '" '; } else { /* * If there's no custom aria-label, we can set a default here. At the * moment it's empty as there's uncertainty about what the default should be. */ $aria_label = ''; } if ( 'html5' === $format ) { $form = '<form role="search" ' . $aria_label . 'method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '"> <label> <span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span> <input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search &hellip;', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" /> </label> <input type="submit" class="search-submit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" /> </form>'; } else { $form = '<form role="search" ' . $aria_label . 'method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '"> <div> <label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label> <input type="text" value="' . get_search_query() . '" name="s" id="s" /> <input type="submit" id="searchsubmit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" /> </div> </form>'; } } /** * Filters the HTML output of the search form. * * @since 2.7.0 * @since 5.5.0 The `$args` parameter was added. * * @param string $form The search form HTML output. * @param array $args The array of arguments for building the search form. * See get_search_form() for information on accepted arguments. */ $result = apply_filters( 'get_search_form', $form, $args ); if ( null === $result ) { $result = $form; } if ( $args['echo'] ) { echo $result; } else { return $result; } }
function get_search_form( $args = array() ) {
	/**
	 * 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
	 * @since 5.5.0 The `$args` parameter was added.
	 *
	 * @link https://core.trac.wordpress.org/ticket/19321
	 *
	 * @param array $args The array of arguments for building the search form.
	 *                    See get_search_form() for information on accepted arguments.
	 */
	do_action( 'pre_get_search_form', $args );

	$echo = true;

	if ( ! is_array( $args ) ) {
		/*
		 * Back compat: to ensure previous uses of get_search_form() continue to
		 * function as expected, we handle a value for the boolean $echo param removed
		 * in 5.2.0. Then we deal with the $args array and cast its defaults.
		 */
		$echo = (bool) $args;

		// Set an empty array and allow default arguments to take over.
		$args = array();
	}

	// Defaults are to echo and to output no custom label on the form.
	$defaults = array(
		'echo'       => $echo,
		'aria_label' => '',
	);

	$args = wp_parse_args( $args, $defaults );

	/**
	 * Filters the array of arguments used when generating the search form.
	 *
	 * @since 5.2.0
	 *
	 * @param array $args The array of arguments for building the search form.
	 *                    See get_search_form() for information on accepted arguments.
	 */
	$args = apply_filters( 'search_form_args', $args );

	// Ensure that the filtered arguments contain all required default values.
	$args = array_merge( $defaults, $args );

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

	/**
	 * Filters the HTML format of the search form.
	 *
	 * @since 3.6.0
	 * @since 5.5.0 The `$args` parameter was added.
	 *
	 * @param string $format The type of markup to use in the search form.
	 *                       Accepts 'html5', 'xhtml'.
	 * @param array  $args   The array of arguments for building the search form.
	 *                       See get_search_form() for information on accepted arguments.
	 */
	$format = apply_filters( 'search_form_format', $format, $args );

	$search_form_template = locate_template( 'searchform.php' );

	if ( '' !== $search_form_template ) {
		ob_start();
		require $search_form_template;
		$form = ob_get_clean();
	} else {
		// Build a string containing an aria-label to use for the search form.
		if ( $args['aria_label'] ) {
			$aria_label = 'aria-label="' . esc_attr( $args['aria_label'] ) . '" ';
		} else {
			/*
			 * If there's no custom aria-label, we can set a default here. At the
			 * moment it's empty as there's uncertainty about what the default should be.
			 */
			$aria_label = '';
		}

		if ( 'html5' === $format ) {
			$form = '<form role="search" ' . $aria_label . 'method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
				<label>
					<span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span>
					<input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search &hellip;', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" />
				</label>
				<input type="submit" class="search-submit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
			</form>';
		} else {
			$form = '<form role="search" ' . $aria_label . 'method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
				<div>
					<label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
					<input type="text" value="' . get_search_query() . '" name="s" id="s" />
					<input type="submit" id="searchsubmit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
				</div>
			</form>';
		}
	}

	/**
	 * Filters the HTML output of the search form.
	 *
	 * @since 2.7.0
	 * @since 5.5.0 The `$args` parameter was added.
	 *
	 * @param string $form The search form HTML output.
	 * @param array  $args The array of arguments for building the search form.
	 *                     See get_search_form() for information on accepted arguments.
	 */
	$result = apply_filters( 'get_search_form', $form, $args );

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

	if ( $args['echo'] ) {
		echo $result;
	} else {
		return $result;
	}
}

常見問題

FAQs
檢視更多 >