Home WordPress General Functions get_search_form() Definition In WordPress, get_search_form() outputs the site search form . If your theme has a searchform.php file, WordPress loads that file. If not, it prints a standard form.
The form sends the visitor to the search results page, where is_search() is true.
Syntax Call it with no arguments to print the form.
<?php get_search_form(); ?> Call What it does get_search_form()Prints the search form. get_search_form( array( 'echo' => false ) )Returns the form as a string. get_search_form( array( 'aria_label' => 'Site search' ) )Adds an accessible label to the form.
To change the look, add a searchform.php file to your theme.
Full declaration get_search_form( array $args = [] ): void|string Parameters Parameter Description $argsarray. Options: echo (bool, default true) and aria_label (string, default empty). Default is an empty array.
Return Value Return Value Description void When echo is true, the form is printed. string When echo is false, the form HTML is returned.
WordPress Examples Three short theme examples. Each one shows the code and what it prints.
1. Show the search form 2. Return the form as a string 3. Add an aria label Example 1 — Show the search form Put a search box in the header.
<div class="header-search">
<?php get_search_form(); ?>
</div> <div class="header-search">
<form role="search" method="get" class="search-form" action="https://example.com/">
<label>
<span class="screen-reader-text">Search for:</span>
<input type="search" class="search-field" placeholder="Search …" value="" name="s">
</label>
<input type="submit" class="search-submit" value="Search">
</form>
</div> How It Works WordPress prints a form that sends the word as s. The action is the home URL. If searchform.php exists, that file is used instead. Example 2 — Return the form as a string Wrap the form in your own markup.
<?php
$form = get_search_form( array( 'echo' => false ) );
echo '<section class="find-us">' . $form . '</section>'; <section class="find-us"><form role="search" ...>...</form></section> How It Works echo is false, so nothing is printed yet.The HTML is stored in $form. We print it inside our own section. Example 3 — Add an aria label Help screen readers.
<?php get_search_form( array( 'aria_label' => 'Site search' ) ); ?> <form role="search" method="get" class="search-form" action="https://example.com/" aria-label="Site search">
...
</form> How It Works aria_label becomes an attribute on the form.Screen readers announce "Site search". This matters when a page has two search forms. Frequently Asked Questions What does get_search_form() do? It prints the site search form, or returns it as a string when echo is false.
How do I change the search form HTML? Create a searchform.php file in your theme. WordPress loads it instead of the default form.
How do I return the form instead of printing? Pass array( 'echo' => false ).
Where do the results show? On the search results page. Use is_search() to check for it in your templates.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion