Function Reference/get search form

来自WordPress中文文档
跳转至: 导航搜索

wordpress.org.cn

Description

Display search form using searchform.php Theme file.

Usage

%%%<?php get_search_form( $echo ); ?>%%%

Parameters

tt$echo/tt
(boolean) (optional) true to echo the form; false to return the form as a string.
Default: truenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

Return Values

(string) 
The form's HTML, if the $echo parameter is set to false.

Examples

If you don't have searchform.php in your Theme, WordPress will render its built-in search form:

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
    <div><label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="Search" />
    </div>
</form>

(A note from Codex reader: the default code above is no longer default (as of WP3.6-beta3). Default code doesn't have id="searchform" but has class="searchform" and if you switch to HTML5 output then it's type="search" and not type="text". If someone knows about all the differences, please update the code and delete my note).

If you do have searchform.php in your Theme, it will be used instead. Keep in mind that the search form should do a GET to the homepage of your blog. The input text field should be named s and you should always include a label like in the example above.

Example of a custom searchform.php:

<form action="/" method="get">
    <fieldset>
        <label for="search">Search in <?php echo home_url( '/' ); ?></label>
        <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
        <input type="image" alt="Search" src="<?php bloginfo( 'template_url' ); ?>/images/search.png" />
    </fieldset>
</form>

A last option is to write a custom function (in your functions.php file) and hook that function to the get_search_form action hook.

function my_search_form( $form ) {
    $form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
    <div><label class="screen-reader-text" for="s">' . __( 'Search for:' ) . '</label>
    <input type="text" value="' . get_search_query() . '" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search' ) .'" />
    </div>
    </form>';

    return $form;
}

add_filter( 'get_search_form', 'my_search_form' );

Notes

The $echo parameter is ignored when searchform.php is present. There is a issue in the WordPress Trac concerning this.

A workaround is to pass the form to get_search_form() through the get_search_form filter.

Please use value="<?php echo get_search_query(); ?>" (so a wrapper function for: esc_attr( $s ); and other necessary filters) if you'd like to display currently searched term in the search field once searching have already occurred. This is one of the most XSS vulnerable places in themes if not secured.

Change Log

Source File

get_search_form() is located in onlyincludecodewp-includes/general-template.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Related

Include Tags: get_header, get_footer, get_sidebar, get_template_part, get_search_form, comments_template,

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude