WP API and WPML

WP API and WPML integration – WPML to WP API plugin

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

Both WPML and WP-API doesn’t want to solve the issue.  Their official answer was that “someone else should build a bridge plugin”.  So here it is!

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
Simple stuff! Download it here: https://wordpress.org/plugins/wpml-to-wp-api/ If you have questions about this plugin, you can also ask us in the comments below.