How to get the Custom flag for a Language using Language Code in WPML

51 Views Asked by At

I am using WPML for multilingual functionality. I want to display the Language Name and Language Flag based on the Language code. I can get the Language Name using the below filter. Here I have used 'de' as an example. This can be any other Language code.

$lang_code = 'de';
$lang_name = apply_filters('wpml_translated_language_name', false, $lang_code );

However, I am not able to retrieve the Flag associated with that language. I found an option to get the Flag using the below code but the issue is that it doesn't work if Custom Flag is configured for the language in the backend.

$flag_url = ICL_PLUGIN_URL . '/res/flags/' . $lang_code . '.svg';

I more thing is I don't want to use any hooks which loops through all the available languages to get the flag for 1 language as it is not the efficient way.

Thanks in advance.

1

There are 1 best solutions below

3
Samuel Krempasky On

There is hook called wpml_active_languages, whose returned array includes for every language value country_flag_url. So you should be able to get url by something like code below:

$languages = apply_filters( 'wpml_active_languages', NULL, 'orderby=id&order=desc' );
$lang_code = 'de';
if (isset($languages[$lang_code]) && isset($languages[$lang_code]['country_flag_url'])) {
    $country_flag_url = $languages[$lang_code]['country_flag_url'];
    echo $country_flag_url;
}