Created
October 20, 2016 12:48
-
-
Save martinsotirov/69e065ab5668f7d4d432156670c52972 to your computer and use it in GitHub Desktop.
WPML - REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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