WP API and WPML doesn’t have an integration plugin. Both sides didn’t want to solve it so I decided to do it. To solve this problem, I came up with a super simple but effective solution.
Requirements
When we rebuilt this site, we had some requirements:
- I wanted to do some cool JS on the frontend to filter out post/custom posts using the new-ish WP-REST-API
- I wanted to retain our old site’s multi-lingualness which I very much like (we’re both bilingual)
Issues
- WPML doesn’t support WP-API: https://wpml.org/forums/topic/make-wpml-compatible-with-official-json-rest-api/
- WP-API doesn’t support WPML: https://wordpress.org/support/topic/wpml-compatibility-20
The Solution
The solution is to use WP-REST-API’s “wp_json_server_before_serve” hook to change out the language.
Here’s a gist of what the plugin does:
- See if there is a “lang” variable set as part of the request
- If nothing is set on lang, set it as the default language.
- Make sure to filter that lang variable. It better exist as an available language for this site.
- If the “lang” variable exists, use that to set the language for the site