wp_dropdown_languages( string|array $args = array() )

Language selector.


Description Description


Parameters Parameters

$args

(string|array) (Optional) Array or string of arguments for outputting the language selector.

  • 'id'
    (string) ID attribute of the select element.
  • 'name'
    (string) Name attribute of the select element.
  • 'languages'
    (array) List of installed languages, contain only the locales.
  • 'translations'
    (array) List of available translations. Default result of wp_get_available_translations().
  • 'selected'
    (string) Language which should be selected.
  • 'echo'
    (bool|int) Whether to echo the generated markup. Accepts 0, 1, or their boolean equivalents. Default 1.
  • 'show_available_translations'
    (bool) Whether to show available translations. Default true.
  • 'show_option_site_default'
    (bool) Whether to show an option to fall back to the site's locale. Default false.

Default value: array()


Top ↑

Return Return

(string) HTML content


Top ↑

Source Source

File: wp-includes/l10n.php

function wp_dropdown_languages( $args = array() ) {

	$args = wp_parse_args( $args, array(
		'id'           => '',
		'name'         => '',
		'languages'    => array(),
		'translations' => array(),
		'selected'     => '',
		'echo'         => 1,
		'show_available_translations' => true,
		'show_option_site_default'    => false,
	) );

	// English (United States) uses an empty string for the value attribute.
	if ( 'en_US' === $args['selected'] ) {
		$args['selected'] = '';
	}

	$translations = $args['translations'];
	if ( empty( $translations ) ) {
		require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
		$translations = wp_get_available_translations();
	}

	/*
	 * $args['languages'] should only contain the locales. Find the locale in
	 * $translations to get the native name. Fall back to locale.
	 */
	$languages = array();
	foreach ( $args['languages'] as $locale ) {
		if ( isset( $translations[ $locale ] ) ) {
			$translation = $translations[ $locale ];
			$languages[] = array(
				'language'    => $translation['language'],
				'native_name' => $translation['native_name'],
				'lang'        => current( $translation['iso'] ),
			);

			// Remove installed language from available translations.
			unset( $translations[ $locale ] );
		} else {
			$languages[] = array(
				'language'    => $locale,
				'native_name' => $locale,
				'lang'        => '',
			);
		}
	}

	$translations_available = ( ! empty( $translations ) && $args['show_available_translations'] );

	$output = sprintf( '<select name="%s" id="%s">', esc_attr( $args['name'] ), esc_attr( $args['id'] ) );

	// Holds the HTML markup.
	$structure = array();

	// List installed languages.
	if ( $translations_available ) {
		$structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
	}

	if ( $args['show_option_site_default'] ) {
		$structure[] = sprintf(
			'<option value="site-default" data-installed="1"%s>%s</option>',
			selected( 'site-default', $args['selected'], false ),
			_x( 'Site Default', 'default site language' )
		);
	}

	$structure[] = sprintf(
		'<option value="" lang="en" data-installed="1"%s>English (United States)</option>',
		selected( '', $args['selected'], false )
	);

	foreach ( $languages as $language ) {
		$structure[] = sprintf(
			'<option value="%s" lang="%s"%s data-installed="1">%s</option>',
			esc_attr( $language['language'] ),
			esc_attr( $language['lang'] ),
			selected( $language['language'], $args['selected'], false ),
			esc_html( $language['native_name'] )
		);
	}
	if ( $translations_available ) {
		$structure[] = '</optgroup>';
	}

	// List available translations.
	if ( $translations_available ) {
		$structure[] = '<optgroup label="' . esc_attr_x( 'Available', 'translations' ) . '">';
		foreach ( $translations as $translation ) {
			$structure[] = sprintf(
				'<option value="%s" lang="%s"%s>%s</option>',
				esc_attr( $translation['language'] ),
				esc_attr( current( $translation['iso'] ) ),
				selected( $translation['language'], $args['selected'], false ),
				esc_html( $translation['native_name'] )
			);
		}
		$structure[] = '</optgroup>';
	}

	$output .= join( "\n", $structure );

	$output .= '</select>';

	if ( $args['echo'] ) {
		echo $output;
	}

	return $output;
}

Top ↑

Changelog Changelog

Changelog
Version Description
4.7.0 Introduced the show_option_site_default argument.
4.3.0 Introduced the echo argument.
4.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.