Skip to Content

search_form()

Modify the WordPress search form to use Bootstrap components.

PHP November 16, 2019

Usage

PHP
nebula()->search_form()

Parameters

$form
(Optional) (String) The HTML form from WordPress
Default: null

$button
(Optional) (Boolean) Whether to show the submit button
Default: true

Parameter Notes

This function is also hooked from the core WordPress get_search_form filter which passes the first parameter as the existing HTML form.

Request or provide clarification »

Additional Notes

This function is used to replace core WordPress search forms (as described above), but also can be called manually.

Source File

Located in /libs/Functions.php on line 1530.

1 Hook

Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

Filters
"pre_nebula_search_form"
Need a new filter hook? Request one here.

Actions
This function has no action hooks available. Request one?

PHP
    public function search_form($form=null, $button=true){
        $override = apply_filters('pre_nebula_search_form', null, $form);
        if ( isset($override) ){return $override;}

        $placeholder = ( get_search_query() )? get_search_query() : __('Search', 'nebula');

        $form = '<form id="searchform" class="form-group form-inline ignore-form" role="search" method="get" action="' . home_url('/') . '">
                    <div class="input-group mb-2 mr-sm-2 mb-sm-0">
                        <div class="input-group-prepend mb-2">
                            <div class="input-group-text"><i class="fas fa-search"></i></div>
                        </div>
                        <label class="sr-only" for="s">Search</label>
                        <input id="s" class="form-control ignore-form mb-2" type="text" name="s" value="' . get_search_query() . '" placeholder="' . $placeholder . '" role="search" />
                    </div>';

        if ( !empty($button) ){
            $form .= '<button id="searchsubmit" class="btn btn-brand wp_search_submit mb-2" type="submit">' . __('Submit', 'nebula') . '</button>';
        }

        $form .= '</form>';

        return $form;
    }

Override

To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

PHP
add_filter('pre_nebula_search_form', 'my_custom_search_form', 10, 3); //The last integer must be 1 more than the actual parameters
function my_custom_search_form($null, $form, $button){ //$null is required, but can be ignored
    //Write your own code here

    return true; //Return true to prevent the original function from running afterwords
}

You can completely disable this PHP function with a single line actions:

PHP
 add_filter('pre_nebula_search_form', '__return_false');