Posted on

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=""> 
<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" /> 

Just replace 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" />
    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?


    • 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 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.

  • yuval shafrir

    hi how can i move the search window from it’s defauls place in my template ? also – how do i get relevanssi to bring results from comments on posts ? thanks

    • I don’t know, the placement of the search box is up to your theme. Relevanssi has nothing to do with it.

      To get results from comments, enable comment indexing in Relevanssi settings and rebuild the index.

  • Heidi Melocco

    Is this what you use to put the actual search bar into your site? Where do you put this to put it in your theme and header? I’d be glad to pay if the search plugin came with a search bar.

    • Heidi Melocco

      I don’t have a search window at all so not sure how to put this in to link to the newly set up Relevanssi.

  • Heidi Melocco

    Just wanted to say thanks– your search is working so much better than the default and what we’ve used before!

    • Thanks. My commenting system ate your other comments, but no, you don’t need to use this shortcode or pay for anything, you can just use the default WordPress search widget that comes with WordPress.