Function Reference/get search form

跳转至: 导航搜索


Display search form using searchform.php Theme file.


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


(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


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..

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

Return Values

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


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" />

(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">
        <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" />

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' ) .'" />

    return $form;

add_filter( 'get_search_form', 'my_search_form' );


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


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


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

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



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


This Template is used by Codex:Template Messages.


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