To answer the titular question, "How to get dashboard langauge not the website language?" the dashboard language is dependent on the currently logged-in user.
The site's language depends on what you've set in Settings > General > Site Language. In a fresh install of WordPress, this language is what is shown on the frontend and in the dashboard and you can get this language using the get_locale()
function.
When you're editing your user profile in Users > Profile, you can choose your own language (or edit another user's profile to change their language). The default setting for new users is Site Default
which is the same as the site's language.
To get the user's language, you can use the get_user_locale()
function. The function also accepts a single parameter, a user ID or WP_User object if you want to get the language from a specific user. The default value of the parameter is 0
which attempts to get the currently logged in user's language, but for anonymous users, it defaults to using get_locale()
.
To answer your more nuanced question, "say the frontend is in English but the dashboard set to German.. i need to get the german" you can try something like this.
Let's say in Settings > General, you have the Administration Email Address set to [email protected]
and Language set to English. Let's also say you have an Administrator user account with the same email and their language is set to German, you could write a function like this:
/**
* Get Site Admin's Language
*
* @return string The locale of the site administrator, or current user on failure.
*/
function au_get_admin_language()
{
$email = sanitize_email(get_bloginfo('admin_email'));
if ($email) {
$user = get_user_by('email', $email);
if ($user !== false) {
return get_user_locale($user);
}
}
return get_user_locale();
}
So when you call au_get_admin_language()
, it'd return German for you.
And if the administration user email address isn't the same as any user account, you can always hardcode it for your case, like $user = get_user_by('email','[email protected]');
or even $user = get_user_by('ID',123);
if you know the user ID.
get_user_locale()
is a right function, and returns the language set for the dashboard for a given user. Are you sure that the user you are checking has set the language for dashboard? If it is not set, the function will return the language of the site (front-end).