Skip to Content
Menu

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.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

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

    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 = '<search><form id="searchform" class="row gx-2 ignore-form" role="search" method="get" action="' . home_url('/') . '">
                            <div class="col">
                                <div class="input-group">
                                    <div class="input-group-text"><i class="fa-solid fa-magnifying-glass"></i></div>
                                    <label class="visually-hidden" for="s">Search</label>
                                    <input id="s" class="form-control ignore-form" type="text" name="s" value="' . get_search_query() . '" placeholder="' . $placeholder . '" role="search" autocorrect="off" autocapitalize="off" spellcheck="false" />
                                </div>
                            </div>';
    
                if ( !empty($button) ){
                    $form .= '<div class="col"><button id="searchsubmit" class="btn btn-brand wp_search_submit mb-2" type="submit">' . __('Submit', 'nebula') . '</button></div>';
                }
    
                $form .= '</form></search>';
    
                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');