問題描述

我試圖在一個德國客户的某些網站上安裝一個小插件。

我可以在德語中填寫 WordPress 的方式,但如果是英文,會更容易。

有一個管理這個 (WP Native Dashboard) 的插件,雖然它奇怪,它對我需要的重量太重了。客户不需要這個,我做。試圖模擬它無效… 它存儲一個數據庫選項來檢查交換而不是 $current_user 。但是我沒有得到這個工作的邏輯。

所以,我正在嘗試改編這個 solution given by toscho,但是看起來我沒有把這些鈎子放在 WordPress 進程的正確點上。

問題是:在下面的代碼中有幾個缺少 (或者我在搞亂)?

<?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();

最佳解決方案

好的,終於到了 WP Native Dashboard 的基本概念的核心,它現在正在工作。

該文件被用作 mu-plugin,每當我在網站上工作時,我將其從 set-user-locale.phpa 重命名為 set-user-locale.php,然後再次重命名。因此激活和停用,而插件不在客户端的視線。

[更新] 遵循 kaiser 的提示,此插件僅在啓動類時定義的用户的插件列表中顯示 (與該語言相同的類) 。該插件現在位於常規插件文件夾的根目錄。

[更新 2] 新版本:僅處理問題的核心。對於隱藏部分,我使用的是 another technique 。由於版本 1.2 在活動時僅具有 auto-hiding 的缺陷。

<?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' ) );

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。