WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/get search form

Contents

Description

Display search form using searchform.php Theme file.

Usage

<?php get_search_form(); ?>

Parameters

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

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>

The above form is used on HTML4 websites. If your theme supports HTML5, which happens if it uses add_theme_support('html5', array('search-form')); it will output the following HTML form. This is the case since WordPress 3.6.

<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
	<label>
		<span class="screen-reader-text">Search for:</span>
		<input type="search" class="search-field" placeholder="Search …" value="" name="s" title="Search for:" />
	</label>
	<input type="submit" class="search-submit" value="Search" />
</form>

Among the changes is that the form has a class="search-form". Also, the input is type="search" and not type="text". Furthermore there is a placeholder, which displays text when appropriate, which means you don't need javascript to display the placeholder. There are no elements with an id anymore, so you can have multiple searchforms in a valid document.

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

The only parameter here that will be submitted is s with the value of the current search query. However, you can refine it in many ways. For example by only showing posts in the search results. This can be done with the next addition to the form above:

<input type="hidden" value="post" name="post_type" id="post_type" />

Here we submit the value post. The default value is any, meaning, posts, pages and custom post types. With the input above added to the form it will now only search in posts with the post_type post. There are many additions like this. With a var_dump of the object $wp_query you can see all the default values of the search variables. With a var_dump of $wp_query->query you can see the current query.


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" class="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 an 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 wp-includes/general-template.php.

Related

Include Tags

See also index of Function Reference and index of Template Tags.