wppaste
WordPress

get_user_locale( int|WP_User $user = 0 ): string

Since
4.7.0
Source
wp-includes/l10n.php:94
Retrieves the locale of a user.

Description

If the user has a locale set to a non-empty string then it will be returned. Otherwise it returns the locale of get_locale().

Compatibility

WordPress
since 4.7.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

$userint|WP_Useroptional
User's ID or a WP_User object. Defaults to current user.Default: 0

Return value

string
The locale of the user.

Performance profile

How much work a call to get_user_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
11–24

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
24

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

What it touches

  • querycontent queryget_user_by()called directly
  • hookthird-party callbacksapply_filters()one call below get_user_locale()
  • optionnetwork optionget_site_option()one call below get_user_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 · 10 distinct outcomes

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

WhenInstructionsCalls it makes
$user !== 0 && $user_object11–13none
$user !== 011–15get_locale()
$user === 0 && !function_exists() && $user_object15–17function_exists()
$user === 0 && !function_exists()15–19function_exists(), get_locale()
$user === 0 && function_exists() && $user_object16function_exists(), wp_get_current_user()
$user === 0 && function_exists()16–18function_exists(), wp_get_current_user(), get_locale()
$user !== 0 && !($user instanceof) && $user && $user_object18get_user_by()
$user !== 0 && !($user instanceof) && $user18–20get_user_by(), get_locale()
$user === 0 && !function_exists() && !($user instanceof) && $user && $user_object22function_exists(), get_user_by()
$user === 0 && !function_exists() && !($user instanceof) && $user22–24function_exists(), get_user_by(), get_locale()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev3411–247
8.53411–247
8.43411–2472 fewer instructions than PHP 8.3
8.33611–267
8.23611–267
8.13611–267
7.43611–267

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 · 3

Used by · 24

Show all 24

Source code

function get_user_locale( $user = 0 ) {	$user_object = false; 	if ( 0 === $user && function_exists( 'wp_get_current_user' ) ) {		$user_object = wp_get_current_user();	} elseif ( $user instanceof WP_User ) {		$user_object = $user;	} elseif ( $user && is_numeric( $user ) ) {		$user_object = get_user_by( 'id', $user );	} 	if ( ! $user_object ) {		return get_locale();	} 	$locale = $user_object->locale; 	return $locale ? $locale : get_locale();}

Changelog

Introduced in 4.7.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 7.1.0 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.