apply_filters( 'wpml_language_codes_map', array $map )
- Type
- filter
- Category
- Site-Wide Language Information
- WPML Version
- 5.0.0
Description
Filters the map of language codes to the codes WPML uses in URLs.
A language has two codes in WPML 5.0. language_code is the stored code, and it never changes. The URL code is the segment a visitor sees, for example /de-de/. The two differ when WPML qualifies the URL code, which happens when you add two countries of one language in the same step, and when the site owner edits the URL code.
This filter is the map the URL converter itself reads, so it can never disagree with the links WPML builds. Use it to learn the address of a language. To read all the data of the active languages, including the flag and the full URL, use wpml_active_languages.
You build the map and WPML rewrites it. WPML replaces the value of every key it knows with that language’s URL code, and it leaves every other key as you passed it. WPML never adds a language, so an empty array comes back empty. Build the map first, as WPML code => WPML code, and then apply the filter. WPML does the same in its own code.
Arguments
- Parameters
-
- $map(array) A map of
WPML code => codethat you build yourself. WPML replaces the value of each key it knows with that language’s URL code. A key WPML does not know keeps the value you passed.
- $map(array) A map of
- Returns
- (array) The same map, with the URL code of each language WPML knows. WPML adds no keys, so pass every language you want to read.
Example usage
Read the map to learn which URL segment a language uses. The example writes one line per language. It reads the language list with wpml_active_languages, so it runs after the wp action, like that filter.
Example
function my_language_url_codes() {
$codes = apply_filters( 'wpml_active_languages', null );
if ( empty( $codes ) ) {
return;
}
$identity = array_combine( array_keys( $codes ), array_keys( $codes ) );
$map = apply_filters( 'wpml_language_codes_map', $identity );
foreach ( $map as $language_code => $url_code ) {
echo esc_html( $language_code . ' is served at /' . $url_code . '/' ) . '<br />';
}
}
To change the URL code of a language, edit it in WPML » Settings » Languages. Do not use this filter to move a language to another address: WPML reads the map when it parses an incoming URL and when it builds a link, so a value that no language owns produces addresses that WPML cannot resolve.
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_active_languages