Get WPML Current Language

User
get wpml current language
Assistant
To get the current language in WPML, you can use the following methods:

### 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? 🚀

User
$current_language = apply_filters( 'wpml_current_language', null );
echo $current_language;
apply check if wpml acitive
Assistant
To ensure that WPML is active before using `apply_filters( 'wpml_current_language', null );`, you can check if the `sitepress-multilingual-cms` plugin is active. Here's a safe way to get the current language only if WPML is active:

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! 🚀

Leave a Comment

Your email address will not be published. Required fields are marked *