wppaste
WordPress

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

Since
4.0.0, 4.3.0, 4.7.0, 5.1.0, 5.9.0
Source
wp-includes/l10n.php:1666
Displays or returns a Language selector.

Compatibility

WordPress
since 5.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$argsstring|arrayoptional
Array or string of arguments for outputting the language selector.Default: array()
  • $idstringdefault: 'locale'

    ID attribute of the select element.
  • $namestringdefault: 'locale'

    Name attribute of the select element.
  • $languagesstring[]default: empty array

    List of installed languages, contain only the locales.
  • $translationsarraydefault: result of wp_get_available_translations()

    List of available translations.
  • $selectedstringdefault: empty

    Language which should be selected.
  • $echobool|intdefault: 1

    Whether to echo the generated markup. Accepts 0, 1, or their boolean equivalents.
  • $show_available_translationsbooldefault: true

    Whether to show available translations.
  • $show_option_site_defaultbooldefault: false

    Whether to show an option to fall back to the site's locale.
  • $show_option_en_usbooldefault: true

    Whether to show an option for English (United States).
  • $explicit_option_en_usbooldefault: false

    Whether the English (United States) option uses an explicit value of en_US instead of an empty value.

Return value

string
HTML dropdown list of languages.

Performance profile

How much work a call to wp_dropdown_languages() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads stored settings via get_site_transient(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
9–133

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 234.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • transienttransientget_site_transient()one call below wp_dropdown_languages()
  • hookthird-party callbacksapply_filters()one call below wp_dropdown_languages()

Further down the call graph this can also reach cache, option, http, serialize and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 31 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_dropdown_languages() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$parsed_args9wp_parse_args()
!empty($translations)56–68wp_parse_args(), esc_attr(), esc_attr()
empty($translations)62–72wp_parse_args(), wp_get_available_translations(), esc_attr(), esc_attr()
!empty($translations)64–81wp_parse_args(), esc_attr_x(), esc_attr(), esc_attr()
empty($translations)70–85wp_parse_args(), wp_get_available_translations(), esc_attr_x(), esc_attr(), esc_attr()
!empty($translations)74–86wp_parse_args(), selected(), _x(), esc_attr(), esc_attr()
!empty($translations)77–90wp_parse_args(), esc_attr(), selected(), esc_attr(), esc_attr()
!empty($translations)79–89wp_parse_args(), esc_attr_x(), esc_attr_x(), esc_attr(), esc_attr()
empty($translations)80–90wp_parse_args(), wp_get_available_translations(), selected(), _x(), esc_attr(), esc_attr()
!empty($translations)82–94wp_parse_args(), esc_attr_x(), selected(), _x(), esc_attr(), esc_attr()
empty($translations)83–94wp_parse_args(), wp_get_available_translations(), esc_attr(), selected(), esc_attr(), esc_attr()
!empty($translations)85–98wp_parse_args(), esc_attr_x(), esc_attr(), selected(), esc_attr(), esc_attr()
19 further outcomes, up to 133 instructions
empty($translations)85–93wp_parse_args(), wp_get_available_translations(), esc_attr_x(), esc_attr_x(), esc_attr(), esc_attr()
empty($translations)88–98wp_parse_args(), wp_get_available_translations(), esc_attr_x(), selected(), _x(), esc_attr(), esc_attr()
!empty($translations)89–99wp_parse_args(), selected(), _x(), esc_attr_x(), esc_attr(), esc_attr()
empty($translations)91–102wp_parse_args(), wp_get_available_translations(), esc_attr_x(), esc_attr(), selected(), esc_attr(), esc_attr()
!empty($translations)92–103wp_parse_args(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr()
$parsed_args && !empty($translations)95–108wp_parse_args(), selected(), _x(), esc_attr(), selected(), esc_attr(), esc_attr()
empty($translations)95–103wp_parse_args(), wp_get_available_translations(), selected(), _x(), esc_attr_x(), esc_attr(), esc_attr()
!empty($translations)97–107wp_parse_args(), esc_attr_x(), selected(), _x(), esc_attr_x(), esc_attr(), esc_attr()
empty($translations)98–107wp_parse_args(), wp_get_available_translations(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr()
!empty($translations)100–111wp_parse_args(), esc_attr_x(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr()
$parsed_args && empty($translations)101–112wp_parse_args(), wp_get_available_translations(), selected(), _x(), esc_attr(), selected(), esc_attr(), esc_attr()
$parsed_args && !empty($translations)103–116wp_parse_args(), esc_attr_x(), selected(), _x(), esc_attr(), selected(), esc_attr(), esc_attr()
empty($translations)103–111wp_parse_args(), wp_get_available_translations(), esc_attr_x(), selected(), _x(), esc_attr_x(), esc_attr(), esc_attr()
empty($translations)106–115wp_parse_args(), wp_get_available_translations(), esc_attr_x(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr()
$parsed_args && empty($translations)109–120wp_parse_args(), wp_get_available_translations(), esc_attr_x(), selected(), _x(), esc_attr(), selected(), esc_attr(), esc_attr()
$parsed_args && !empty($translations)110–121wp_parse_args(), selected(), _x(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr()
$parsed_args && empty($translations)116–125wp_parse_args(), wp_get_available_translations(), selected(), _x(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr()
$parsed_args && !empty($translations)118–129wp_parse_args(), esc_attr_x(), selected(), _x(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr()
$parsed_args && empty($translations)124–133wp_parse_args(), wp_get_available_translations(), esc_attr_x(), selected(), _x(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2349–13320
8.52349–13320
8.42349–133201 more instruction than PHP 8.3
8.32339–13620
8.22339–13620
8.12339–13620
7.42339–13620

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 7

Used by · 2

Source code

function wp_dropdown_languages( $args = array() ) { 	$parsed_args = wp_parse_args(		$args,		array(			'id'                          => 'locale',			'name'                        => 'locale',			'languages'                   => array(),			'translations'                => array(),			'selected'                    => '',			'echo'                        => 1,			'show_available_translations' => true,			'show_option_site_default'    => false,			'show_option_en_us'           => true,			'explicit_option_en_us'       => false,		)	); 	// Bail if no ID or no name.	if ( ! $parsed_args['id'] || ! $parsed_args['name'] ) {		return;	} 	// English (United States) uses an empty string for the value attribute.	if ( 'en_US' === $parsed_args['selected'] && ! $parsed_args['explicit_option_en_us'] ) {		$parsed_args['selected'] = '';	} 	$translations = $parsed_args['translations'];	if ( empty( $translations ) ) {		require_once ABSPATH . 'wp-admin/includes/translation-install.php';		$translations = wp_get_available_translations();	} 	/*	 * $parsed_args['languages'] should only contain the locales. Find the locale in	 * $translations to get the native name. Fall back to locale.	 */	$languages = array();	foreach ( $parsed_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 ) && $parsed_args['show_available_translations'] ); 	// Holds the HTML markup.	$structure = array(); 	// List installed languages.	if ( $translations_available ) {		$structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';	} 	// Site default.	if ( $parsed_args['show_option_site_default'] ) {		$structure[] = sprintf(			'<option value="site-default" data-installed="1"%s>%s</option>',			selected( 'site-default', $parsed_args['selected'], false ),			_x( 'Site Default', 'default site language' )		);	} 	if ( $parsed_args['show_option_en_us'] ) {		$value       = ( $parsed_args['explicit_option_en_us'] ) ? 'en_US' : '';

Changelog

Introduced in 5.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.9.0
Introduced the explicit_option_en_us argument.from the docblock
5.1.0
Introduced the show_option_en_us argument.from the docblock
4.7.0
Introduced the show_option_site_default argument.from the docblock
4.3.0
Introduced the echo argument.from the docblock
4.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/l10n.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.