The WPML plugin is a popular choice for creating multilingual websites in WordPress. By default, WPML uses language codes like “en” for English, “fr” for French, and so on. However, there may be cases where you want to use custom language codes instead. In this case, you can use the icl_get_languages()
function provided by WPML to customize the language switcher output.
Here’s an example code snippet that demonstrates how to customize the language switcher to display custom language codes:
function wpsnippets_custom_language_code( $languages ) {
// Add or modify language codes as needed
$languages['en']['language_code'] = 'english';
$languages['fr']['language_code'] = 'french';
$languages['de']['language_code'] = 'german';
return $languages;
}
add_filter( 'icl_get_languages', 'wpsnippets_custom_language_code' );
In this example, we’re using the icl_get_languages
filter hook to modify the language codes returned by the icl_get_languages()
function. The function wpsnippets_custom_language_code()
is hooked into this filter and accepts the $languages
array as a parameter. We can then add or modify language codes within this array.
Note that the language codes are added or modified using the language’s slug as the array key. For example, to modify the language code for English, we use $languages['en']['language_code'] = 'english';
.
By using this code snippet, you can customize the language codes displayed in the WPML language switcher to match your specific requirements.
Examples
Example 1: Adding custom language code to WPML language switcher
This example demonstrates how to add a custom language code to the WPML language switcher. By default, WPML uses the ISO 639-1 language codes for language switcher URLs. However, you may want to use custom language codes instead.
function wpsnippets_custom_language_code( $languages ) {
$languages['fr'] = 'fr-FR';
$languages['es'] = 'es-ES';
return $languages;
}
add_filter( 'icl_ls_languages', 'wpsnippets_custom_language_code' );
In this code example, we use the icl_ls_languages
filter to modify the language codes used by WPML. The wpsnippets_custom_language_code
function adds custom language codes for French and Spanish. The language codes are defined as key-value pairs in the $languages
array. The key represents the custom language code, and the value represents the corresponding WPML language code.
Example 2: Removing a language from WPML language switcher
This example demonstrates how to remove a specific language from the WPML language switcher. You may want to remove a language if it is not relevant to your website or if you want to simplify the language switcher.
function wpsnippets_remove_language( $languages ) {
unset( $languages['de'] );
return $languages;
}
add_filter( 'icl_ls_languages', 'wpsnippets_remove_language' );
In this code example, we use the icl_ls_languages
filter to remove the German language from the WPML language switcher. The wpsnippets_remove_language
function removes the ‘de’ language code from the $languages
array using the unset
function.
Example 3: Reordering languages in WPML language switcher
This example demonstrates how to change the order of languages in the WPML language switcher. By default, WPML displays languages in alphabetical order. However, you may want to reorder the languages based on your preference.
function wpsnippets_reorder_languages( $languages ) {
$new_order = array( 'en', 'fr', 'es', 'it', 'ja' );
$reordered_languages = array();
foreach ( $new_order as $code ) {
if ( isset( $languages[ $code ] ) ) {
$reordered_languages[ $code ] = $languages[ $code ];
unset( $languages[ $code ] );
}
}
return array_merge( $reordered_languages, $languages );
}
add_filter( 'icl_ls_languages', 'wpsnippets_reorder_languages' );
In this code example, we use the icl_ls_languages
filter to reorder the languages in the WPML language switcher. The wpsnippets_reorder_languages
function defines a new order for the languages in the $new_order
array. We then iterate through the $new_order
array and move the corresponding language codes to the $reordered_languages
array. Finally, we merge the $reordered_languages
array with the original $languages
array to maintain the order of the remaining languages.