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

Note: For WPML versions >=3.2 please use the wpml_add_language_form_field action hook. The function shown below can still be used but it has been deprecated and will be removed completely in 3.3

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); ?>"/>

19 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?

  7. Gerard says:

    I still have this very same problem with the latest version, any ideas? :S

    • Amir says:

      Can you post about this in our technical forum? Our support folks will be able to help. Explain exactly what you’re doing and what you’re getting. If you can post a link, they will be able to help you much better.

  8. AJ Clarke says:

    Wouldn’t it be better to use template_redirect to redirect search results to the correct language? This way if people have custom searchforms on the site it would always work?

    • Amir says:

      AJ, what you’re describing would certainly be a valid solution. However, it’s implemented like we describe it in WPML and works just fine. Your custom search form should still work with WPML, as long as the language attribute is included and you are using the WordPress API. WPML filters WordPress API calls.

  9. David Plunkett says:

    This does not seem to work for other languages, the default returns fine, but when I try say German for example the URL shows the root and not the german domain e.g. de

    I am using this:

    Which causes the url to always be:

    http://localhost:3000/?s=searchquery&lang=de

    When I want it to be:

    http://localhost:3000/de/?s=searchquery&lang=de

    • Agnes Bury says:

      Hi David, I checked it with our developers and they say that should work fine. If it is not working as you expect, please report it in our support forum explaining how to replicate your case.

  10. yutthakan says:

    I used Real Homes ,real estate theme ,How to config ?

    • Agnes Bury says:

      The Real Home is compatible with WPML and was tested by our Compatibility team.
      https://wpml.org/theme/real-homes/

      You configure it to work with WPML the same way you would set up WPML to work with any other theme. If you get lost, use our documentation or support help. To translate your theme’s custom option the String translation module might be needed, so best is to choose at least the “Multilingual CMS” subscription. You can upgrade to the Lifetime subscription later.

  11. yutthakanI says:

    How to edit Search Field to search 3 languages. ?

    <input type="text" name="keyword" id="keyword-txt"
    value="”
    placeholder=””/>

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=""> <s> <strike> <strong>