Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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: http://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();
share|improve this question
    
Try admin_init. Then drop is_admin(); and see if $current_user does really contain a sub object named data. –  kaiser May 25 '12 at 19:05
    
@kaiser - nope, admin_init neither - and yes, $current_user is populated, I use FirePHP for debugging... thanks! –  brasofilo May 25 '12 at 19:52
    
Ok, I asked if $current_user->data is populated :) Plus: does it work w/o the checks? –  kaiser May 25 '12 at 19:56
    
@kaiser - I've modified the code in the question - It works if I don't check for the $current_user - It's quite funny because the info is there... –  brasofilo May 25 '12 at 20:10
    
@kaiser - got it working, what do you think? Thanks! –  brasofilo May 29 '12 at 21:30
add comment

1 Answer

up vote 5 down vote accepted

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: http://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' ) );
share|improve this answer
    
Wouldn't just click de-/activate be easier? ;) Fine seeing it work. +1 –  kaiser May 29 '12 at 22:07
1  
@kaiser - nice inspiration but ended using this other filter... and updated the code, now it's smooth and ready to roll ;o) –  brasofilo May 29 '12 at 23:15
1  
This is reeeeally neat. I wonder if it would work for WPMS? This would be awesome for multilingual teams. Thanks! –  moraleida May 31 '12 at 20:39
1  
@moraleida : yes, it does! Just tested as a mu-plugin and with two super-admin users. –  brasofilo May 31 '12 at 20:58
1  
Awesome! I'd upvote this twice if i could. :) –  moraleida May 31 '12 at 21:01
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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