Get WPML Current Language
February 6, 2025 · AI Conversations
### 1. **Using `ICL_LANGUAGE_CODE` Constant**
You can use the predefined constant `ICL_LANGUAGE_CODE` anywhere in your theme or plugin:
echo ICL_LANGUAGE_CODE; // Outputs: en, fr, de, etc.
### 2. **Using WPML API Function `wpml_get_current_language()`**
If you're using the WPML API, use:
$current_language = apply_filters( 'wpml_current_language', null );
echo $current_language;
### 3. **Using WPML’s `wpml_get_language_information()` Function** *(Since WPML 4.5)*
$language_info = wpml_get_language_information();
echo $language_info['language_code'];
### 4. **Using `$_GET` for URL-based Language Detection**
If WPML is using URL parameters (`?lang=en`), you can grab it like this:
$current_language = isset($_GET['lang']) ? sanitize_text_field($_GET['lang']) : 'default_language';
echo $current_language;
Would you like this integrated into a function or shortcode? 🚀
echo $current_language;
apply check if wpml acitive
if ( function_exists( 'icl_get_languages' ) ) {
$current_language = apply_filters( 'wpml_current_language', null );
echo $current_language;
} else {
echo 'WPML is not active';
}
### Alternative Check Using `is_plugin_active()`
If you want a more strict check using WordPress' `is_plugin_active()`, you can do this:
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
$current_language = apply_filters( 'wpml_current_language', null );
echo $current_language;
} else {
echo 'WPML is not active';
}
This ensures your code doesn't break if WPML is disabled or not installed. Let me know if you need further refinements! 🚀