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
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 arrayList of installed languages, contain only the locales.$translationsarraydefault: result of wp_get_available_translations()List of available translations.$selectedstringdefault: emptyLanguage which should be selected.$echobool|intdefault: 1Whether to echo the generated markup. Accepts 0, 1, or their boolean equivalents.$show_available_translationsbooldefault: trueWhether to show available translations.$show_option_site_defaultbooldefault: falseWhether to show an option to fall back to the site's locale.$show_option_en_usbooldefault: trueWhether to show an option for English (United States).$explicit_option_en_usbooldefault: falseWhether 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
- Scaling
- Scales with input
- Instructions
- 9–133
- Plugin surface
- None
- Called by
- 2
Reads stored settings via get_site_transient(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 234.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- transienttransient
get_site_transient()one call below wp_dropdown_languages() - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$parsed_args | 9 | wp_parse_args() |
!empty($translations) | 56–68 | wp_parse_args(), esc_attr(), esc_attr() |
empty($translations) | 62–72 | wp_parse_args(), wp_get_available_translations(), esc_attr(), esc_attr() |
!empty($translations) | 64–81 | wp_parse_args(), esc_attr_x(), esc_attr(), esc_attr() |
empty($translations) | 70–85 | wp_parse_args(), wp_get_available_translations(), esc_attr_x(), esc_attr(), esc_attr() |
!empty($translations) | 74–86 | wp_parse_args(), selected(), _x(), esc_attr(), esc_attr() |
!empty($translations) | 77–90 | wp_parse_args(), esc_attr(), selected(), esc_attr(), esc_attr() |
!empty($translations) | 79–89 | wp_parse_args(), esc_attr_x(), esc_attr_x(), esc_attr(), esc_attr() |
empty($translations) | 80–90 | wp_parse_args(), wp_get_available_translations(), selected(), _x(), esc_attr(), esc_attr() |
!empty($translations) | 82–94 | wp_parse_args(), esc_attr_x(), selected(), _x(), esc_attr(), esc_attr() |
empty($translations) | 83–94 | wp_parse_args(), wp_get_available_translations(), esc_attr(), selected(), esc_attr(), esc_attr() |
!empty($translations) | 85–98 | wp_parse_args(), esc_attr_x(), esc_attr(), selected(), esc_attr(), esc_attr() |
19 further outcomes, up to 133 instructions
empty($translations) | 85–93 | wp_parse_args(), wp_get_available_translations(), esc_attr_x(), esc_attr_x(), esc_attr(), esc_attr() |
empty($translations) | 88–98 | wp_parse_args(), wp_get_available_translations(), esc_attr_x(), selected(), _x(), esc_attr(), esc_attr() |
!empty($translations) | 89–99 | wp_parse_args(), selected(), _x(), esc_attr_x(), esc_attr(), esc_attr() |
empty($translations) | 91–102 | wp_parse_args(), wp_get_available_translations(), esc_attr_x(), esc_attr(), selected(), esc_attr(), esc_attr() |
!empty($translations) | 92–103 | wp_parse_args(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr() |
$parsed_args && !empty($translations) | 95–108 | wp_parse_args(), selected(), _x(), esc_attr(), selected(), esc_attr(), esc_attr() |
empty($translations) | 95–103 | wp_parse_args(), wp_get_available_translations(), selected(), _x(), esc_attr_x(), esc_attr(), esc_attr() |
!empty($translations) | 97–107 | wp_parse_args(), esc_attr_x(), selected(), _x(), esc_attr_x(), esc_attr(), esc_attr() |
empty($translations) | 98–107 | wp_parse_args(), wp_get_available_translations(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr() |
!empty($translations) | 100–111 | wp_parse_args(), esc_attr_x(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr() |
$parsed_args && empty($translations) | 101–112 | wp_parse_args(), wp_get_available_translations(), selected(), _x(), esc_attr(), selected(), esc_attr(), esc_attr() |
$parsed_args && !empty($translations) | 103–116 | wp_parse_args(), esc_attr_x(), selected(), _x(), esc_attr(), selected(), esc_attr(), esc_attr() |
empty($translations) | 103–111 | wp_parse_args(), wp_get_available_translations(), esc_attr_x(), selected(), _x(), esc_attr_x(), esc_attr(), esc_attr() |
empty($translations) | 106–115 | wp_parse_args(), wp_get_available_translations(), esc_attr_x(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr() |
$parsed_args && empty($translations) | 109–120 | wp_parse_args(), wp_get_available_translations(), esc_attr_x(), selected(), _x(), esc_attr(), selected(), esc_attr(), esc_attr() |
$parsed_args && !empty($translations) | 110–121 | wp_parse_args(), selected(), _x(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr() |
$parsed_args && empty($translations) | 116–125 | wp_parse_args(), wp_get_available_translations(), selected(), _x(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr() |
$parsed_args && !empty($translations) | 118–129 | wp_parse_args(), esc_attr_x(), selected(), _x(), esc_attr(), selected(), esc_attr_x(), esc_attr(), esc_attr() |
$parsed_args && empty($translations) | 124–133 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 234 | 9–133 | 20 | |
| 8.5 | 234 | 9–133 | 20 | |
| 8.4 | 234 | 9–133 | 20 | 1 more instruction than PHP 8.3 |
| 8.3 | 233 | 9–136 | 20 | |
| 8.2 | 233 | 9–136 | 20 | |
| 8.1 | 233 | 9–136 | 20 | |
| 7.4 | 233 | 9–136 | 20 |
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
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_get_available_translations()Get available translations from the WordPress.org API.
- esc_attr_x()Translates string with gettext context, and escapes it for safe use in an attribute.
- selected()Outputs the HTML selected attribute.
- _x()Retrieves translated string with gettext context.
- esc_attr()Escaping for HTML attributes.
- esc_html()Escaping for HTML blocks.
Used by · 2
- login_footer()Outputs the footer for the login page.
- show_blog_form()Generates and displays the Sign-up and Create Site forms.
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.
Signature, return type and hooks compared across 5 parsed releases.
explicit_option_en_us argument.from the docblockshow_option_en_us argument.from the docblockshow_option_site_default argument.from the docblockecho argument.from the docblockAbout 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.