apply_filters( 'wpml_active_languages', mixed $empty_value, array|string $args )
- Type
- filter
- Category
- Site-Wide Language Information
- WPML Version
- 3.2
Description
Filters the list of the languages enabled (active) for a site. This filter is usually used to create custom language switchers.
This filter can only work when the global $wp_query object has been instantiated. This means that it should be called after the wp action happens.
WPML returns a site’s active (enabled) languages as an array with relative information per language. For example, for a WordPress site running English, French and Italian, the hook will return this:
Array ( [en] => Array ( [id] => 1 [active] => 1 [default_locale] => en_US [native_name] => English [missing] => 0 [translated_name] => English [language_code] => en [country_flag_url] => http://yourdomain/wpmlpath/res/flags/en.png [url] => http://yourdomain/about ) [fr] => Array ( [id] => 4 [active] => 0 [default_locale] => fr_FR [native_name] => Français [missing] => 0 [translated_name] => French [language_code] => fr [country_flag_url] => http://yourdomain/wpmlpath/res/flags/fr.png [url] => http://yourdomain/fr/a-propos ) [it] => Array ( [id] => 27 [active] => 0 [default_locale] => it_IT [native_name] => Italiano [missing] => 0 [translated_name] => Italian [language_code] => it [country_flag_url] => http://yourdomain/wpmlpath/res/flags/it.png [url] => http://yourdomain/it/circa ) )
The wpml_active_languages filter provides a way to do things like reorder the language array. Or something more complex, like redirecting missing languages to a custom url.
Arguments
- $empty_value
- (mixed) (Required) This is normally the value the filter will be modifying. We are not filtering anything here so set this to NULL. This for the filter function to actually receive the full argument list
- $args
- (array|string) (Optional) Arguments to filter the language output
- skip_missing(bool) How to treat languages with no translations.
1to skip language or0to link to home of language for missing translations. - link_empty_to(string) Works in conjunction with skip_missing = 0 and allows using custom links for the languages that do not have translations for the current element.
{%lang}can be used as placeholder for the language code. Empty by default. - orderby(string) Accepts
id|code|nameDefaults to custom. The custom order can be defined in the WordPress admin under WPML » Languages » Language Switcher Options - order(string) Accepts
asc|desc
- skip_missing(bool) How to treat languages with no translations.
Example usage
The filter’s arguments can be passed to it as a string as shown in the first example below. They can also be passed as an array as you can see in the second example further down.
In the example below we display a flag only language switcher. However, there is a small difference to the flag-only language switcher you can configure from your “WPML language settings” in the admin backend.
The difference here is that the current language will not be wrapped inside an anchor. Only the page’s alternative languages will have a link. Have a look.
Example
function my_flag_only_language_switcher() {
$languages = apply_filters( 'wpml_active_languages', NULL, 'orderby=id&order=desc' );
if ( !empty( $languages ) ) {
foreach( $languages as $l ) {
if ( !$l['active'] ) echo ' <a href="' . esc_url( $l['url'] ) . '">';
echo '<img src="' . esc_url ( $l['country_flag_url'] ) . '" height="12" alt="' . esc_attr( $l['language_code'] ) . '" width="18" />';
if ( !$l['active'] ) echo '</a> ';
}
}
}
In the next example you see below, we redirect missing languages to a custom page where we display a contact form for people to notify the site admin of untranslated content.
We also capitalize the language name of the active language i.e. the language we are currently viewing, using PHP’s
strtoupper function.
A second example
function my_custom_language_switcher() {
$languages = apply_filters( 'wpml_active_languages', NULL, array( 'skip_missing' => 0, 'link_empty_to' => 'http://domain.com/missing-translation-contact-form' ) );
if( !empty( $languages ) ) {
foreach( $languages as $language ){
$native_name = $language['active'] ? strtoupper( $language['native_name'] ) : $language['native_name'];
if( !$language['active'] ) echo '<a href="' . esc_url( $language['url'] ) . '">';
echo esc_html( $native_name ) . ' ';
if( !$language['active'] ) echo '</a>';
}
}
}
Related hooks
wpml_decode_custom_field, wpml_footer_language_selector, wpml_add_language_form_field, wpml_ls_exclude_in_menu, wpml_is_rtl, wpml_alternate_hreflang, wpml_head_langs, wpml_must_translate_canonical_url, wpml_browser_redirect_language_params, wpml_encode_custom_field