The get_search_form() WordPress PHP function displays the search form on your website.
Usage
get_search_form( array(
'echo' => true,
'aria_label' => 'Search this website'
) );
Parameters
- $args (array, optional) – Array of display arguments:
- echo (bool) – Whether to echo or return the form. Default is true.
- aria_label (string) – ARIA label for the search form. Useful to distinguish multiple search forms on the same page and improve accessibility.
More information
See WordPress Developer Resources: get_search_form
Examples
Display search form with default settings
The following code displays the search form with the default settings.
get_search_form();
Display search form with a custom ARIA label
This example demonstrates how to set a custom ARIA label for the search form.
get_search_form( array(
'aria_label' => 'Search our blog'
) );
Return search form as a string
This example shows how to return the search form as a string instead of echoing it.
$search_form = get_search_form( array(
'echo' => false
) );
Display multiple search forms on the same page
The following example demonstrates how to display two search forms on the same page with different ARIA labels.
get_search_form( array(
'aria_label' => 'Search articles'
) );
get_search_form( array(
'aria_label' => 'Search products'
) );
Customize search form using a filter
This example shows how to customize the search form HTML using the get_search_form filter.
function custom_search_form( $form ) {
// Customize the search form HTML here
return $form;
}
add_filter( 'get_search_form', 'custom_search_form' );