Change admin language based on user (in single-site)

Ok, finally got to the core of WP Native Dashboard basic concept and it’s working now.

The file is being used as a mu-plugin, and whenever I have to work in the site I rename it from set-user-locale.phpa to set-user-locale.php, and then back again. Thus activating and deactivating without the plugin being on the client’s sight.

[update]
Following kaiser’s hint, this plugin only shows up in the plugins list for the user defined when initiating the class (the same one for which the language is changed).
The plugin is now located at the root of the regular plugins folder.

[update 2]
New version: deals only with the core of the question. For the hiding part I’m using another technique. As the version 1.2 had the flaw of only auto-hiding when active.

<?php
/*
Plugin Name: Admin interface in English for selected users
Plugin URI: https://wordpress.stackexchange.com/a/52436/12615
Description: Edit this file to add/remove users from the list
Version: 1.5
Author: Rodolfo Buaiz
*/

class Wpse53326_ChangeLocaleOnDemand
{

    public function __construct( $the_user )
    {       
        $this->user = $the_user;
        add_filter( 'locale', array( $this, 'on_change_language' ) );
   }

    public function on_change_language( $loc )
    {
        if ( !is_admin() )
         return $loc;

        if ( function_exists( 'wp_get_current_user' ) ) 
        {
            $u = wp_get_current_user();
            if ( !isset($u->user_locale) ) 
            {
                if ( in_array( $u->data->user_login, $this->user ) )
                    $u->user_locale="";
                else
                    $u->user_locale="de_DE";
            }
            return $u->user_locale;
        }

        return $loc;
    }

}

new Wpse53326_ChangeLocaleOnDemand( array( 'user1', 'User2' ) );

Leave a Comment