WordPress get_search_form() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Template loader

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.

WordPress
<?php get_search_form(); ?>
CallWhat 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
WordPress
get_search_form( array $args = [] ): void|string

Parameters

ParameterDescription
$argsarray. Options: echo (bool, default true) and aria_label (string, default empty). Default is an empty array.

Return Value

Return ValueDescription
voidWhen echo is true, the form is printed.
stringWhen echo is false, the form HTML is returned.

WordPress Examples

Three short theme examples. Each one shows the code and what it prints.

Example 1 — Show the search form

Put a search box in the header.

WordPress
<div class="header-search">
  <?php get_search_form(); ?>
</div>

How It Works

  1. WordPress prints a form that sends the word as s.
  2. The action is the home URL.
  3. If searchform.php exists, that file is used instead.

Example 2 — Return the form as a string

Wrap the form in your own markup.

WordPress
<?php
$form = get_search_form( array( 'echo' => false ) );
echo '<section class="find-us">' . $form . '</section>';

How It Works

  1. echo is false, so nothing is printed yet.
  2. The HTML is stored in $form.
  3. We print it inside our own section.

Example 3 — Add an aria label

Help screen readers.

WordPress
<?php get_search_form( array( 'aria_label' => 'Site search' ) ); ?>

How It Works

  1. aria_label becomes an attribute on the form.
  2. Screen readers announce "Site search".
  3. This matters when a page has two search forms.

Frequently Asked Questions

It prints the site search form, or returns it as a string when echo is false.
Create a searchform.php file in your theme. WordPress loads it instead of the default form.
Pass array( 'echo' => false ).
On the search results page. Use is_search() to check for it in your templates.

Next: get_sidebar()

Load sidebar.php or a named sidebar file such as sidebar-shop.php.

get_sidebar() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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