How to Have Different Admin and Theme Languages in WordPress

Home » Snippets » How to Have Different Admin and Theme Languages in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

One of the great things about WordPress is that it can be used by people all over the world, regardless of their language. However, sometimes you may want to have a different language for the WordPress admin dashboard than for the theme of your website. In this article, we’ll show you how to easily set different languages for the admin and theme of your WordPress site. So, let’s dive in!

					function wpturbo_admin_theme_language() {
    if ( is_admin() ) {
        $language = get_user_meta( get_current_user_id(), 'wp_user_language', true );
        if ( $language ) {
            $locale = $language . '.mo';
            $mofile = WP_LANG_DIR . '/plugins/' . $locale;
            load_textdomain( 'default', $mofile );
        }
    } else {
        $language = get_locale();
        $locale = $language . '.mo';
        $mofile = WP_LANG_DIR . '/themes/' . $locale;
        load_theme_textdomain( 'wpturbo', $mofile );
    }
}
add_action( 'plugins_loaded', 'wpturbo_admin_theme_language' );
				

The code snippet is designed to enable different languages in the WordPress admin area and the theme area. This feature can be useful if your theme or plugin will be used by users who speak different languages.

The wpturbo_admin_theme_language() function is responsible for setting the language for both the admin and the theme areas. First, it checks if the user is in the admin area using is_admin(). If it is, the function retrieves the language preference for the current user from the user meta using the get_user_meta() function.

if ( is_admin() ) {
    $language = get_user_meta( get_current_user_id(), 'wp_user_language', true );

If a language preference is available for the user, the function constructs the locale and the mofile (mo file) using the user’s language preference. The mofile is the path to the language file for the plugin in the "wp-content/languages/plugins" folder.

if ( $language ) {
    $locale = $language . '.mo';
    $mofile = WP_LANG_DIR . '/plugins/' . $locale;

The function then loads the text domain for the default domain, which is usually used for the WordPress core strings and other default strings in the plugin. The text domain is loaded with the load_textdomain() function. The mofile variable is passed as the second parameter, which loads the language file based on the user’s language preference.

load_textdomain( 'default', $mofile );

If the user is not in the admin area, the function sets the language for the theme area. It first retrieves the current language using the get_locale() function. The function constructs the locale and the mofile in the same way as for the admin area. The load_theme_textdomain() function is then used to load the language file based on the theme’s text domain.

} else {
    $language = get_locale();
    $locale = $language . '.mo';
    $mofile = WP_LANG_DIR . '/themes/' . $locale;
    load_theme_textdomain( 'wpturbo', $mofile );
}

The last step is to hook the wpturbo_admin_theme_language() function into the plugins_loaded action, which ensures that the language setting is initialized as early as possible during WordPress’ loading process.

Register an account to save your snippets or go Pro to get more features.