Using WordPress ‘get_search_form’ PHP filter

The get_search_form WordPress PHP filter allows you to modify the HTML output of the search form.

Usage

add_filter('get_search_form', 'your_custom_function', 10, 2);
function your_custom_function($form, $args) {
    // your custom code here
    return $form;
}

Parameters

  • $form (string) – The search form HTML output.
  • $args (array) – The array of arguments for building the search form. See get_search_form() for information on accepted arguments.

More information

See WordPress Developer Resources: get_search_form

Examples

Change the search form placeholder text

This example changes the search form input field’s placeholder text to “Search my site…”:

add_filter('get_search_form', 'change_search_placeholder', 10, 2);
function change_search_placeholder($form, $args) {
    $form = str_replace('placeholder="Search"', 'placeholder="Search my site..."', $form);
    return $form;
}

Add a CSS class to the search form

This example adds a custom CSS class to the search form element:

add_filter('get_search_form', 'add_custom_search_class', 10, 2);
function add_custom_search_class($form, $args) {
    $form = str_replace('class="search-form"', 'class="search-form my-custom-search"', $form);
    return $form;
}

Modify the search form submit button text

This example changes the search form submit button text to “Find”:

add_filter('get_search_form', 'change_search_submit_text', 10, 2);
function change_search_submit_text($form, $args) {
    $form = str_replace('value="Search"', 'value="Find"', $form);
    return $form;
}

Wrap the search form in a custom HTML element

This example wraps the search form in a custom <div> element with a specific class:

add_filter('get_search_form', 'wrap_search_form', 10, 2);
function wrap_search_form($form, $args) {
    $form = '<div class="custom-search-wrapper">' . $form . '</div>';
    return $form;
}

Remove the search form label

This example removes the label element from the search form for a cleaner appearance:

add_filter('get_search_form', 'remove_search_form_label', 10, 2);
function remove_search_form_label($form, $args) {
    $form = preg_replace('/<label.*?>.*?<\/label>/', '', $form);
    return $form;
}