I am trying to customize the default WordPress search field. So I thought I could add a filter to remove the function and then add in a new function with the fields the way I want them. So I tried this

 function savior_search(){
     <form role="search" method="get" id="searchform" action="<?php echo home_url( '/');        ?>">
    <input type="text" value="" name="s" id="s" />
<?php }

 add_filter( 'get_search_form', 'savior_search');

It seems to work but for some reason that I don't understand, it duplicates the search field. It puts the form on the page twice. I also commented out get_search_form in the sidebar yet when I refresh my page, it's still there. I am using the filter incorrectly? My search function is in the functions.php.


Your syntax is wrong, you're mixing up the html and php function at the beginning.

See Filter here - wp-includes/general-template.php#L151

I think we should return the form, instead of printing it, See my example, It should work, If you want to modify something, do it in function.

Example -

function savior_search(){

    //Modify This
    $form = '<form role="search" method="get" id="searchform" action="' . esc_url( home_url( '/' ) ) . '" >
                <div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
                    <input type="text" value="' . get_search_query() . '" name="s" id="s" />
                    <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />

    return $form;

add_filter( 'get_search_form', 'savior_search');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.