get_user_locale( int|WP_User $user = 0 ): string
- Since
- 4.7.0
- Source
wp-includes/l10n.php:94
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
- Scaling
- Constant
- Instructions
- 11–24
- Plugin surface
- None
- Called by
- 24
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 34.
Nothing here hands control to plugin code.
24 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_user_by()called directly - hookthird-party callbacks
apply_filters()one call below get_user_locale() - optionnetwork option
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
$user !== 0 && $user_object | 11–13 | none |
$user !== 0 | 11–15 | get_locale() |
$user === 0 && !function_exists() && $user_object | 15–17 | function_exists() |
$user === 0 && !function_exists() | 15–19 | function_exists(), get_locale() |
$user === 0 && function_exists() && $user_object | 16 | function_exists(), wp_get_current_user() |
$user === 0 && function_exists() | 16–18 | function_exists(), wp_get_current_user(), get_locale() |
$user !== 0 && !($user instanceof) && $user && $user_object | 18 | get_user_by() |
$user !== 0 && !($user instanceof) && $user | 18–20 | get_user_by(), get_locale() |
$user === 0 && !function_exists() && !($user instanceof) && $user && $user_object | 22 | function_exists(), get_user_by() |
$user === 0 && !function_exists() && !($user instanceof) && $user | 22–24 | function_exists(), get_user_by(), get_locale() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 34 | 11–24 | 7 | |
| 8.5 | 34 | 11–24 | 7 | |
| 8.4 | 34 | 11–24 | 7 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 36 | 11–26 | 7 | |
| 8.2 | 36 | 11–26 | 7 | |
| 8.1 | 36 | 11–26 | 7 | |
| 7.4 | 36 | 11–26 | 7 |
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
- wp_get_current_user()Retrieves the current user object.
- get_user_by()Retrieves user info by a given field.
- get_locale()Retrieves the current locale.
Used by · 24
- WP_Community_Events::get_request_args()Builds an array of args to use in an HTTP request to the w.org Events API.
- WP_Debug_Data::get_wp_core()Gets the WordPress core section of the debug data.
- WP_Locale_Switcher::switch_to_user_locale()Switches the translations according to the given user's locale.
- WP_Plugin_Install_List_Table::prepare_items()
- WP_REST_Pattern_Directory_Controller::get_items()Search and retrieve block patterns metadata
- WP_REST_Users_Controller::prepare_item_for_response()Prepares a single user output for response.
- WP_Theme::sort_by_name()Sorts themes by name.
- _WP_Editors::editor_settings()
- _WP_Editors::get_mce_locale()Returns the TinyMCE locale.
- determine_locale()Determines the current locale desired for the request.
- hello_dolly()
- iframe_header()Generic Iframe header for use with Thickbox.
Show all 24
- plugins_api()Retrieves plugin installer pages from the WordPress.org Plugins API.
- retrieve_password()Handles sending a password retrieval email to a user.
- themes_api()Retrieves theme installer pages from the WordPress.org Themes API.
- wp_auth_check_html()Outputs the HTML that shows the wp-login dialog when the user is no longer logged in.
- wp_credits()Retrieves the contributor credits.
- wp_dashboard_browser_nag()Displays the browser update nag.
- wp_dashboard_cached_rss_widget()Checks to see if all of the feed url in $check_urls are cached.
- wp_dashboard_rss_control()Sets up the RSS dashboard widget control and $args to be used as input to wp_widget_rss_form().
- wp_default_packages_inline_scripts()Adds inline scripts required for the WordPress JavaScript packages.
- wp_default_packages_vendor()Registers all the WordPress vendor scripts that are in the standardized `js/dist/vendor/` location.
- wp_get_popular_importers()Returns a list from WordPress.org of popular importer plugins.
- wp_register_core_abilities()Registers the default core abilities.
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.
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.