Skip to content

Instantly share code, notes, and snippets.

@martinsotirov
Created October 20, 2016 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinsotirov/69e065ab5668f7d4d432156670c52972 to your computer and use it in GitHub Desktop.
Save martinsotirov/69e065ab5668f7d4d432156670c52972 to your computer and use it in GitHub Desktop.
WPML - REST API
/**
* Handle WPML REST requests
*/
add_action('rest_api_init', function() {
if (isset($_REQUEST['lang']) && !empty($_REQUEST['lang'])) {
global $sitepress;
$availableLangs = array_keys(apply_filters('wpml_active_languages', []));
$currentLang = get_query_var('lang');
$newLang = sanitize_text_field($_REQUEST['lang']);
if ($newLang !== $currentLang && in_array($newLang, $availableLangs)) {
$sitepress->switch_lang($newLang);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment