0

Sometimes you may have a language for the dashboard that differs from the site language, how to get the dashboard language. i tried to use get_locale and get_user_locale but both get the site language.

say the frontend is in English but the dashboard set to German.. i need to get the german

2
  • 1
    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).
    – nmr
    Jun 5, 2019 at 16:33
  • I will check again, thank you
    – Makiomar
    Jun 6, 2019 at 9:43

2 Answers 2

0

get_locale() and get_user_locale() are functions to retrieve the locale that is already set. To change it, you want to "filter" the locale value.

For that, you want to use the locale filter hook. The following should do it:

add_filter( 'locale', 'custom_locale' );
function custom_locale( $locale ) {

    $locale = ( is_admin() ) ? "en_US" : "de_DE";
    return $locale;

}

This should set the admin locale to English, otherwise display German.

4
  • Thank you, but actaully i need to get the admin language not to set it, when i use get_locale it gets the language of site on frontend while i ned the language of the backend.. say the frontend is in English but the dashboard set to German.. i need to get the german
    – Makiomar
    Jun 5, 2019 at 15:22
  • 1
    Well, get_locale() will tell you what the locale is set to. But there is no difference in "frontend" and "backend." You just have one locale for the site. That's not to say that you can change it for one or the other, but that's a filtering process (whether you create the filter or it's done some other way, such as a plugin). Also, get_locale() may be run more than once, so if it is changed, that may be later rather than sooner, so get_locale() may return a different result depending on when/where you run it.
    – butlerblog
    Jun 5, 2019 at 16:10
  • To get a successful answer, you might need to change your question to something more specific about what you are doing and what the current use case actually is (more detail than you currently have).
    – butlerblog
    Jun 5, 2019 at 16:12
  • Ok thank you, I will
    – Makiomar
    Jun 6, 2019 at 9:41
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.