Skip Navigation
availability:

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.

type:
filter
category:
Site-Wide Language Information
parameters:
apply_filters( 'wpml_active_languages', mixed $empty_value, array|string $args )
$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. 1 to skip language or 0 to 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|name Defaults to custom. The custom order can be defined in the WordPress admin under WPML » Languages » Language Switcher Options
  • order(string) Accepts asc|desc
hook 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>';
        }
    }
}