wppaste
WordPress

determine_locale(): string

Since
5.0.0
Source
wp-includes/l10n.php:123
Determines the current locale desired for the request.

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

Reaches the database via get_user_by().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9–60

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

Plugin surface
2 hooks

Third-party callbacks on 'pre_determine_locale', 'determine_locale' run inside this call, and their cost is not bounded by anything here.

Called by
11

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

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_user_by()one call below determine_locale()
  • optionnetwork optionget_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.

WhenInstructionsCalls it makes
is_string($determined_locale)9apply_filters()
is_admin()21–32apply_filters(), is_admin(), get_user_locale(), apply_filters()
is_admin()24–35apply_filters(), is_admin(), get_user_locale(), get_locale(), apply_filters()
!isset($value) && !is_admin() && empty($value)25–40apply_filters(), is_admin(), apply_filters()
!is_admin()26–48apply_filters(), is_admin(), wp_installing(), apply_filters()
!isset($value) && !is_admin() && empty($value)28–43apply_filters(), is_admin(), get_locale(), apply_filters()
!is_admin()29–51apply_filters(), is_admin(), wp_installing(), get_locale(), apply_filters()
isset($value) && !empty($value)31–36apply_filters(), sanitize_locale_name(), apply_filters()
!is_admin() && isset($value) && wp_is_json_request()31–42apply_filters(), is_admin(), wp_is_json_request(), get_user_locale(), apply_filters()
!is_admin() && !wp_is_json_request() && empty($value)32–43apply_filters(), is_admin(), wp_is_json_request(), apply_filters()
!is_admin() && isset($value) && !wp_is_json_request()33–51apply_filters(), is_admin(), wp_is_json_request(), wp_installing(), apply_filters()
isset($value) && !empty($value)34–39apply_filters(), sanitize_locale_name(), get_locale(), apply_filters()
7 further outcomes, up to 60 instructions
!is_admin() && isset($value) && wp_is_json_request()34–45apply_filters(), is_admin(), wp_is_json_request(), get_user_locale(), get_locale(), apply_filters()
!is_admin() && !wp_is_json_request() && empty($value)35–46apply_filters(), is_admin(), wp_is_json_request(), get_locale(), apply_filters()
!is_admin() && isset($value) && !wp_is_json_request()36–54apply_filters(), is_admin(), wp_is_json_request(), wp_installing(), get_locale(), apply_filters()
!is_admin() && !empty($value) && wp_installing()37–54apply_filters(), is_admin(), wp_installing(), sanitize_locale_name(), apply_filters()
!is_admin() && !empty($value) && wp_installing()40–57apply_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–57apply_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–60apply_filters(), is_admin(), wp_is_json_request(), wp_installing(), sanitize_locale_name(), get_locale(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev869–6016
8.5869–6016
8.4869–6016
8.3869–6016
8.2869–6016
8.1869–60164 fewer instructions than PHP 7.4
7.4909–6316

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:

  1. apply_filters( pre_determine_locale )filterline 133 (+10 into the body)

    Filters the locale for the current request prior to the default determination process.

  2. apply_filters( determine_locale )filterline 175 (+52 into the body)

    Filters the locale for the current request.

Uses · 7

Used by · 11

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.

  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.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 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.