Using WordPress ‘pre_get_search_form’ PHP action

The pre_get_search_form WordPress PHP action fires before the search form is retrieved, at the start of get_search_form().

Usage

add_action('pre_get_search_form', 'your_custom_function', 10, 1);

function your_custom_function($args) {
    // Your custom code here

    return $args;
}

Parameters

  • $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: pre_get_search_form

Examples

Modify the ARIA label

Modify the ARIA label for the search form.

add_action('pre_get_search_form', 'modify_aria_label', 10, 1);

function modify_aria_label($args) {
    $args['aria_label'] = 'Custom ARIA label';
    return $args;
}

Change the search form to be returned instead of echoed

Change the search form to be returned instead of echoed.

add_action('pre_get_search_form', 'return_search_form', 10, 1);

function return_search_form($args) {
    $args['echo'] = false;
    return $args;
}

Add a custom class to the search form

Add a custom class to the search form.

add_action('pre_get_search_form', 'add_custom_class', 10, 1);

function add_custom_class($args) {
    $args['class'] = 'custom-search-form';
    return $args;
}

Modify the search form placeholder text

Modify the search form placeholder text.

add_action('pre_get_search_form', 'modify_search_placeholder', 10, 1);

function modify_search_placeholder($args) {
    $args['placeholder'] = 'Enter your custom search query';
    return $args;
}

Change the search button text

Change the search button text.

add_action('pre_get_search_form', 'change_search_button_text', 10, 1);

function change_search_button_text($args) {
    $args['submit_label'] = 'Find it!';
    return $args;
}