Using WordPress ‘mu_dropdown_languages’ PHP filter

The mu_dropdown_languages WordPress PHP filter allows you to modify the list of languages available in the dropdown menu.

Usage

add_filter('mu_dropdown_languages', 'your_custom_function', 10, 3);

function your_custom_function($output, $lang_files, $current) {
    // your custom code here
    return $output;
}

Parameters

  • $output (string[]): Array of HTML output for the dropdown.
  • $lang_files (string[]): Array of available language files.
  • $current (string): The current language code.

More information

See WordPress Developer Resources: mu_dropdown_languages

Examples

Add a new language to the dropdown

Add the custom language “Esperanto” to the dropdown.

add_filter('mu_dropdown_languages', 'add_esperanto_to_dropdown', 10, 3);

function add_esperanto_to_dropdown($output, $lang_files, $current) {
    $esperanto_option = '<option value="eo" ' . selected('eo', $current, false) . '>Esperanto</option>';
    array_push($output, $esperanto_option);
    return $output;
}

Remove a specific language from the dropdown

Remove the “Spanish” language from the dropdown.

add_filter('mu_dropdown_languages', 'remove_spanish_from_dropdown', 10, 3);

function remove_spanish_from_dropdown($output, $lang_files, $current) {
    $spanish_key = array_search('<option value="es_ES" ' . selected('es_ES', $current, false) . '>Español</option>', $output);
    if ($spanish_key !== false) {
        unset($output[$spanish_key]);
    }
    return $output;
}

Change the display text of a language

Change the display text of the “English” language to “American English”.

add_filter('mu_dropdown_languages', 'change_english_display_text', 10, 3);

function change_english_display_text($output, $lang_files, $current) {
    $english_key = array_search('<option value="en_US" ' . selected('en_US', $current, false) . '>English</option>', $output);
    if ($english_key !== false) {
        $output[$english_key] = '<option value="en_US" ' . selected('en_US', $current, false) . '>American English</option>';
    }
    return $output;
}

Sort languages alphabetically

Sort the languages in the dropdown alphabetically.

add_filter('mu_dropdown_languages', 'sort_languages_alphabetically', 10, 3);

function sort_languages_alphabetically($output, $lang_files, $current) {
    asort($output);
    return $output;
}

Remove all languages except for the current one

Remove all languages from the dropdown except for the current one.

add_filter('mu_dropdown_languages', 'remove_all_except_current', 10, 3);

function remove_all_except_current($output, $lang_files, $current) {
    foreach ($output as $key => $option) {
        if (strpos($option, 'value="' . $current . '"') === false) {
            unset($output[$key]);
        }
    }
    return $output;
}