Using WordPress ‘lang_codes’ PHP filter

The lang_codes WordPress PHP Filter allows you to modify the language codes array.

Usage

add_filter('lang_codes', 'your_custom_function', 10, 2);

function your_custom_function($lang_codes, $code) {
    // your custom code here
    return $lang_codes;
}

Parameters

  • $lang_codes (array): An array of key/value pairs of language codes where the key is the short version.
  • $code (string): A two-letter designation of the language.

More information

See WordPress Developer Resources: lang_codes

Examples

Add a custom language code

Add a custom language code to the language codes array.

add_filter('lang_codes', 'add_custom_language_code', 10, 2);

function add_custom_language_code($lang_codes, $code) {
    $lang_codes['xx'] = 'Custom Language';
    return $lang_codes;
}

Remove a language code

Remove a specific language code from the language codes array.

add_filter('lang_codes', 'remove_language_code', 10, 2);

function remove_language_code($lang_codes, $code) {
    unset($lang_codes['fr']);
    return $lang_codes;
}

Modify a language code

Change the display name for a specific language code.

add_filter('lang_codes', 'modify_language_code', 10, 2);

function modify_language_code($lang_codes, $code) {
    if ($code == 'en') {
        $lang_codes[$code] = 'English (Modified)';
    }
    return $lang_codes;
}

Filter language codes by condition

Filter the language codes array based on a condition (e.g., only European languages).

add_filter('lang_codes', 'filter_language_codes', 10, 2);

function filter_language_codes($lang_codes, $code) {
    $european_languages = ['en', 'fr', 'de', 'es', 'it'];

    return array_intersect_key($lang_codes, array_flip($european_languages));
}

Sort language codes alphabetically

Sort the language codes array alphabetically by their display names.

add_filter('lang_codes', 'sort_language_codes', 10, 2);

function sort_language_codes($lang_codes, $code) {
    asort($lang_codes);
    return $lang_codes;
}