determine_locale(): string
- Since
- 5.0.0
- Source
wp-includes/l10n.php:124
Compatibility
- WordPress
- since 5.0.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.
Return value
string- The determined locale.
Performance profile
How much work a call to determine_locale() 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
- Constant
- Instructions
- 9–60
- Plugin surface
- 2 hooks
- Called by
- 11
Reaches the database via get_user_by().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 86.
Third-party callbacks on 'pre_determine_locale', 'determine_locale' run inside this call, and their cost is not bounded by anything here.
11 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_user_by()one call below determine_locale() - optionnetwork option
get_site_option()one call below determine_locale()
Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 19 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost determine_locale() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_string($determined_locale) | 9 | apply_filters() |
is_admin() | 21–32 | apply_filters(), is_admin(), get_user_locale(), apply_filters() |
is_admin() | 24–35 | apply_filters(), is_admin(), get_user_locale(), get_locale(), apply_filters() |
!isset($value) && !is_admin() && empty($value) | 25–40 | apply_filters(), is_admin(), apply_filters() |
!is_admin() | 26–48 | apply_filters(), is_admin(), wp_installing(), apply_filters() |
!isset($value) && !is_admin() && empty($value) | 28–43 | apply_filters(), is_admin(), get_locale(), apply_filters() |
!is_admin() | 29–51 | apply_filters(), is_admin(), wp_installing(), get_locale(), apply_filters() |
isset($value) && !empty($value) | 31–36 | apply_filters(), sanitize_locale_name(), apply_filters() |
!is_admin() && isset($value) && wp_is_json_request() | 31–42 | apply_filters(), is_admin(), wp_is_json_request(), get_user_locale(), apply_filters() |
!is_admin() && !wp_is_json_request() && empty($value) | 32–43 | apply_filters(), is_admin(), wp_is_json_request(), apply_filters() |
!is_admin() && isset($value) && !wp_is_json_request() | 33–51 | apply_filters(), is_admin(), wp_is_json_request(), wp_installing(), apply_filters() |
isset($value) && !empty($value) | 34–39 | apply_filters(), sanitize_locale_name(), get_locale(), apply_filters() |
7 further outcomes, up to 60 instructions
!is_admin() && isset($value) && wp_is_json_request() | 34–45 | apply_filters(), is_admin(), wp_is_json_request(), get_user_locale(), get_locale(), apply_filters() |
!is_admin() && !wp_is_json_request() && empty($value) | 35–46 | apply_filters(), is_admin(), wp_is_json_request(), get_locale(), apply_filters() |
!is_admin() && isset($value) && !wp_is_json_request() | 36–54 | apply_filters(), is_admin(), wp_is_json_request(), wp_installing(), get_locale(), apply_filters() |
!is_admin() && !empty($value) && wp_installing() | 37–54 | apply_filters(), is_admin(), wp_installing(), sanitize_locale_name(), apply_filters() |
!is_admin() && !empty($value) && wp_installing() | 40–57 | apply_filters(), is_admin(), wp_installing(), sanitize_locale_name(), get_locale(), apply_filters() |
!is_admin() && isset($value) && !wp_is_json_request() && !empty($value) && wp_installing() | 44–57 | apply_filters(), is_admin(), wp_is_json_request(), wp_installing(), sanitize_locale_name(), apply_filters() |
!is_admin() && isset($value) && !wp_is_json_request() && !empty($value) && wp_installing() | 47–60 | apply_filters(), is_admin(), wp_is_json_request(), wp_installing(), sanitize_locale_name(), get_locale(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 86 | 9–60 | 16 | |
| 8.5 | 86 | 9–60 | 16 | |
| 8.4 | 86 | 9–60 | 16 | |
| 8.3 | 86 | 9–60 | 16 | |
| 8.2 | 86 | 9–60 | 16 | |
| 8.1 | 86 | 9–60 | 16 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 90 | 9–63 | 16 |
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.
Hooks and filters fired · 2
2 hooks fire while determine_locale() runs, in this order:
- apply_filters( pre_determine_locale )filterline 134 (+10 into the body)
Filters the locale for the current request prior to the default determination process.
- apply_filters( determine_locale )filterline 176 (+52 into the body)
Filters the locale for the current request.
Uses · 7
- apply_filters()Calls the callback functions that have been added to a filter hook.
- sanitize_locale_name()Strips out all characters not allowed in a locale name.
- is_admin()Determines whether the current request is for an administrative interface page.
- wp_is_json_request()Checks whether current request is a JSON request, or is expecting a JSON response.
- get_user_locale()Retrieves the locale of a user.
- wp_installing()Checks or sets whether WordPress is in "installation" mode.
- get_locale()Retrieves the current locale.
Used by · 11
- WP_Locale_Switcher::__construct()Constructor.
- WP_Locale_Switcher::switch_to_locale()Switches the translations according to the given locale.
- WP_REST_Site_Health_Controller::load_admin_textdomain()Loads the admin textdomain for Site Health tests.
- _get_path_to_translation_from_lang_dir()Gets the path to a translation file in the languages directory for the current locale.
- _load_script_textdomain_from_src()Resolves and loads the translation JSON file for a given script or script module source URL.
- _load_textdomain_just_in_time()Loads plugin and theme text domains just-in-time.
- get_bloginfo()Retrieves information about the current site.
- load_default_textdomain()Loads default translated strings based on locale.
- load_textdomain()Loads a .mo file into the text domain $domain.
- login_footer()Outputs the footer for the login page.
- wp_default_scripts()Registers all WordPress scripts.
Source code
function determine_locale() { /** * Filters the locale for the current request prior to the default determination process. * * Using this filter allows to override the default logic, effectively short-circuiting the function. * * @since 5.0.0 * * @param string|null $locale The locale to return and short-circuit. Default null. */ $determined_locale = apply_filters( 'pre_determine_locale', null ); if ( $determined_locale && is_string( $determined_locale ) ) { return $determined_locale; } if ( isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] && ( ! empty( $_GET['wp_lang'] ) || ! empty( $_COOKIE['wp_lang'] ) ) ) { if ( ! empty( $_GET['wp_lang'] ) ) { $determined_locale = sanitize_locale_name( $_GET['wp_lang'] ); } else { $determined_locale = sanitize_locale_name( $_COOKIE['wp_lang'] ); } } elseif ( is_admin() || ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) ) { $determined_locale = get_user_locale(); } elseif ( ( ! empty( $_REQUEST['language'] ) || isset( $GLOBALS['wp_local_package'] ) ) && wp_installing() ) { if ( ! empty( $_REQUEST['language'] ) ) { $determined_locale = sanitize_locale_name( $_REQUEST['language'] ); } else { $determined_locale = $GLOBALS['wp_local_package']; } } if ( ! $determined_locale ) { $determined_locale = get_locale(); } /** * Filters the locale for the current request. * * @since 5.0.0 * * @param string $determined_locale The locale. */ return apply_filters( 'determine_locale', $determined_locale );}Changelog
Introduced in 5.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.