Using WordPress ‘mu_dropdown_languages()’ PHP function

The mu_dropdown_languages() WordPress PHP function generates and displays a drop-down list of available languages.

Usage

mu_dropdown_languages($lang_files, $current);

Custom example:

Input:

mu_dropdown_languages(['en_US', 'es_ES'], 'en_US');

Output:

<select>
  <option value="en_US" selected="selected">English (US)</option>
  <option value="es_ES">Español</option>
</select>

Parameters

  • $lang_files (string, optional) – An array of the language files. Default: array().
  • $current (string, optional) – The current language code. Default: ”.

More information

See WordPress Developer Resources: mu_dropdown_languages

Examples

Basic example

Display a simple drop-down list of two languages, English and Spanish, with English as the default selected language.

$lang_files = ['en_US', 'es_ES'];
$current = 'en_US';

mu_dropdown_languages($lang_files, $current);

Displaying all available languages

Get all the available languages from the languages directory and display them in a drop-down list.

$lang_files = get_available_languages();
$current = get_locale();

mu_dropdown_languages($lang_files, $current);

Adding a custom language

Add a custom language to the drop-down list along with other available languages.

$lang_files = get_available_languages();
$current = get_locale();

// Add a custom language to the list
$lang_files[] = 'custom_lang';

mu_dropdown_languages($lang_files, $current);

Displaying languages without a selected option

Show a drop-down list of languages without any pre-selected option.

$lang_files = ['en_US', 'es_ES'];
$current = '';

mu_dropdown_languages($lang_files, $current);

Displaying languages in alphabetical order

Show a drop-down list of languages sorted alphabetically by their language codes.

$lang_files = ['en_US', 'fr_FR', 'es_ES'];
sort($lang_files);
$current = 'en_US';

mu_dropdown_languages($lang_files, $current);