Search form
WordPress includes built in Search functionality, which WPML makes multilingual.
For this to work, two things are required in the search form:
- The form needs an ID of searchform.
- 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); ?>"/>
English