Adding a search form shortcode

There’s no shortcode in Relevanssi that would add a search form to a page.

Adding a search form to just about anywhere is simple, though. It looks something like this:

<form role="search" method="get" id="searchform" class="searchform" action="http://www.example.com/"> 
<label class="screen-reader-text" for="s">Search for:</label> 
<input type="text" value="" name="s" id="s" /> 
<input type="submit" id="searchsubmit" value="Search" /> 
</form>

Just replace http://www.example.com/ with the URL of your site. Do note that if you’re adding this to a post or page, the editor must be in “Text” mode, not “Visual”, otherwise the mark-up will be messed up.

Having the search form as a shortcode does make that easier… So, here’s how you do a shortcode out of this:

add_shortcode('searchform', 'rlv_search_form'); 
function rlv_search_form() {
    $url = get_site_url();
    $form = <<<EOH
<form role="search" method="get" id="searchform" class="searchform" action="$url">
<label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
EOH;
    return $form;
}

Add the code to your theme functions.php, and then you’ll be able to invoke the search form with [searchform]. It’ll probably look a bit plain, but you can style it with some CSS to make it look nicer.

  • PJ

    Hi, tried to use the functions.php code above and got an error on the line:

    $form = <<<EOH

    ?

    Any ideas?

    Thanks

    • If you copy and paste from the page, looks like extra spaces get included. Remove spaces from the end of all lines.

  • Hello, how can I make text Search disappears when you click on the container. I put value = Search but this does not go away when you click above must be deleted manually. Thx!!

  • Johnny Rodriguez

    how do you add the custom post type filter to this?

    • Just add hidden-type input field with the name “post_type” and value that matches the post type.

      • B2 Marketing Ltd

        I have seen this tip at least 10 times now but for some reason it is not working. I am wondering if WP has changed something. For example if I test this on your site https://www.relevanssi.com/?s=download&post_type=page then I am still also getting “posts” in the search results. Any idea why?

  • Maru

    Hello, I´m working with ACF and Relevanssi. I created 2 filds:

    [acf field=”locality”]

    [acf field=”city”]

    What I need is the site search form allow search by search: term + city + locality.

    Could you please help me?

    Thank you so much!!

    • Nothing in the search form is necessary, just make Relevanssi index custom fields “locality” and “city”, and searches will find posts by locality and city.

      • Maru

        Thank for the quick response! What I need is that you can search by:
        search Term + city + town (3 search fields at the same time and not one). Thanks in advance”

        • Sure you can do that; it’s just a bit more complicated to do, and doesn’t improve the use experience that much more.

          You’d need to write some code that reads in the city and locality fields and creates a meta_query out of them and adds it to the search parameters.