Using WordPress ‘get_available_languages’ PHP filter

The get_available_languages WordPress PHP filter allows you to modify the list of available language codes in your WordPress site.

Usage

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

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

Parameters

  • $languages (string[]): An array of available language codes.
  • $dir (string): The directory where the language files were found.

More information

See WordPress Developer Resources: get_available_languages

Examples

Add a custom language

Add a custom language code to the list of available languages.

add_filter('get_available_languages', 'add_custom_language', 10, 2);

function add_custom_language($languages, $dir) {
    $languages[] = 'my-custom-language';
    return $languages;
}

Remove a specific language

Remove the Spanish language code from the list of available languages.

add_filter('get_available_languages', 'remove_spanish_language', 10, 2);

function remove_spanish_language($languages, $dir) {
    if(($key = array_search('es_ES', $languages)) !== false) {
        unset($languages[$key]);
    }
    return $languages;
}

Only allow specific languages

Restrict the list of available languages to English and French.

add_filter('get_available_languages', 'allow_specific_languages', 10, 2);

function allow_specific_languages($languages, $dir) {
    return array('en_US', 'fr_FR');
}

Change language directory

Change the directory where the language files are found.

add_filter('get_available_languages', 'change_language_directory', 10, 2);

function change_language_directory($languages, $dir) {
    $new_dir = '/your/new/directory';
    return get_available_languages($new_dir);
}

Sort languages alphabetically

Sort the list of available languages alphabetically.

add_filter('get_available_languages', 'sort_languages_alphabetically', 10, 2);

function sort_languages_alphabetically($languages, $dir) {
    sort($languages);
    return $languages;
}