I want to change the language of my WordPress installation based on a choice the user can make in his profile (dropdown menu).

As I want to change frontend (prepared .mo files for the theme) and backend language I thought I could change the $locale variable and it would work out.

So I came up with the following code in my functions.php but that won´t do the job:

function change_lang() {
   $lang = get_user_meta($current_user->ID, 'user_lang');
   $locale = $lang[0];
   return $locale;
add_filter('locale', 'change_lang');

When I return the value of $locale, WP returns a simple NULL, nothing more. When I release the hook, the defined value from WP_LANG() is returned.

So, I am obviously wrong somewhere, but I can´t find where. Any hints?

Besides: I read this for inspiration and code, but I don´t want to have it as a plugin if possible.

Thanks in advance!

Edit: Fixed my code, but still no language change happens.


Your use of $current_user is a little wrong, you should include the global $current_user or assign it to a variable, or shorter and cleaner just get_current_user_id():

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

   if( $lang = get_user_meta( get_current_user_id(), 'user_lang', true) ) {
       return $lang;

   return $locale;

| improve this answer | |
  • Thank you, I fixed my code. But still no language change at all. Any more ideas? – flomei Jan 19 '15 at 12:45
  • If you have implemented the user meta field "user_lang" and teh user has set the language, this code should work. Have you implemented that field? – cybmeta Jan 19 '15 at 14:08
  • I have, see this pastebin for my code: pastebin.com/v4pmV0xu Also, showing the selected language in the frontend works, so updating etc. is doing fine. – flomei Jan 26 '15 at 11:48
  • 1
    @kaiser coded a user language switcher, may he can say to us why this code is not working. I've been trying to find what can be wrong with no success. – cybmeta Feb 6 '15 at 17:13
  • Thank you very much for pointing that out. I just had a look at the code, but I´m somehow just missing, where he sets the new locale. – flomei Feb 9 '15 at 7:52

Did you mean this option ?

enter image description here

If yes, then...

$user_locale = get_user_locale();
function change_userlang_by_userlocale($locale){
    global $user_locale;
    return $user_locale ? $user_locale : $locale;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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