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 . It’ll probably look a bit plain, but you can style it with some CSS to make it look nicer.

Update: This shortcode is built in in Relevanssi Premium 2.0 and later versions.

23 comments Adding a search form shortcode

  1. 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!!

      1. 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”

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

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

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

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

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

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

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

  5. I added the search form in text editor on a page and it works fab! Im just wondering, is there a way to make the search bar longer? I dont know css or any form of coding, but just used the code to add in text mode on wordpress and it worked a treat. What do i need to add to change the length (make it longer). Thank you very much

    1. Tanya, the lenght of the input field is controlled with a size parameter. Like this:

      <input type="text" value="" name="s" id="s" size="40" />

      Adjust the value to your liking.

Leave a Reply

Your email address will not be published. Required fields are marked *