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

I’m trying to make a small plugin to install in some sites of a german client.

I can make my way around WordPress in german, but would be easier if it was in english.

Read More

There’s a plugin that manages this (WP Native Dashboard) and albeit it does it wonderfully, it’s too heavy weight to what I need. The client don’t need this, I do.
Tried to emulate it to no avail… It stores a database option to check for swapping instead of $current_user. But I didn’t get the logic for this to work.

So, I’m trying to adapt this solution given by toscho, but looks like I’m not making the hooks in the correct points of WordPress process.

The question is: what bit is missing (or I’m messing) in the following code?

<?php
/*
Plugin Name: Set User Locale
Plugin URI: https://wordpress.stackexchange.com/q/53326/12615
Description: changes the admin language according to user_login
Version: 1.0
Author: wordpress-stackexchange
*/

class Wpse53326_ChangeLocaleOnDemand
{

    public function __construct()
    {       
        add_action('admin_init', array(&$this, 'on_init'));
        add_filter( 'locale', array(&$this, 'on_change_language') );
    }

    public function on_init()
    {
    }

    public function on_change_language( $locale )
    {
        global $current_user;       

        // this prints the current user_login without problems 
        // global $firephp; 
        // $firephp->log($current_user->data->user_login,'user_login');

        //  the following works for backend/frontend
        // but if I try this conditional, it don't: if (is_admin() && 'the_user_login' == $current_user->data->user_login)
        if( is_admin() )
        {
            return 'en_US';         
        }
        return $locale;
    }
}

$wpse53326_ChangeLocaleOnDemand_instance = new Wpse53326_ChangeLocaleOnDemand();

Related posts

Leave a Reply

1 comment

  1. 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' ) );