Search form

WordPress includes built in Search functionality, which WPML makes multilingual.

For this to work, two things are required in the search form:

  1. The form needs an ID of searchform.
  2. An input named lang must be set to the current language.

For example, this is the HTML from our own search form:

<form action="http://wpml.org/" id="searchform" method="get">
  <input type="text" id="s" name="s" value=""/>
  <input type="submit" value="Search" id="searchsubmit"/>
  <input type="hidden" name="lang" value="en"/>
</form>

The lang hidden input tells WPML that we’re searching in English. If it was set to ‘es’, it would mean we’re searching in Spanish.

Adding the language attribute to the search form

When the searchform ID is set for the form and the lang input exists, WPML limits the search to that language and shows the search results page in the correct language. You can try our own search in different languages and see the results.

WPML’s sitepress.js does this little magic. If your theme prevents this JS from loading, you can do it yourself by manually adding the lang input to search forms and setting its value to the ICL_LANGUAGE_CODE constant.

<input type="hidden" name="lang" value="<?php echo(ICL_LANGUAGE_CODE); ?>"/>

10 Responses to “Search form”

  1. Erik says:

    It does not work for me :S:S:S:S it still shows posts in both languages.

  2. Erik says:

    I checked your source AND the search-engine of this website and even the search-engine of this website does not work correctly. If I’m right, you’ve used above solution as well, but when I go to the German page and I’m searching for ‘test’, I still get English results. Even on my blog the solution as told above does not work, it’s very very very frustrating. That it shows the results of other languages is not the biggest problem, but in my sidebar I’ve used a custom query to show the latest 5 posts. On all pages this works fine, but when I use the search-engine, it starts showing translated posts. Can someone help me please??????

  3. Emerson says:

    Any progress on this? I can confirm on my end that the search seems to be returning all languages – even with the language params that get passed in.

  4. ThaClown says:

    I have the same issue…
    Any idea when you can privide the fix?

  5. ThaClown says:

    Thanks alot, works perfectly now!

  6. Lod says:

    I am using the latest version with wordpress 3.0 and custom types.The search displays results for all languages.Any help or ideas?

Leave a Reply

Please leave here comments about this page only.
For technical support and feature suggestions, head to our forum. We are waiting there!

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>