get_user_locale()WP 4.7.0

Retrieves the locale of a user.

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().

1 time — 0.00003 sec (very fast) | 50000 times — 0.15 sec (very fast) | PHP 7.0.14, WP 4.7

No Hooks.

Return

String. The locale of the user.

Usage

get_user_locale( $user );
$user(int|WP_User)
User's ID or a WP_User object.
Default: current user

Examples

0

#1 Get the translation language (locale) of the current user

$local = get_user_locale();
echo $local; //> ru_RU
0

#2 Get the translation language of the user with ID 1

$local = get_user_locale( 1 );
echo $local; //> en_US

Changelog

Since 4.7.0 Introduced.

get_user_locale() code WP 6.5.3

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();
}