WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For a single site wordpress, the language must be set from wp-config.php's WPLANG, but is it possible to set from my plugin which override the default value?

share|improve this question
The link also appears at the right column of this page, but I think it's worth noting in the comments another approach for a similar situation: wordpress.stackexchange.com/a/53678/12615 – brasofilo May 31 '12 at 21:00
up vote 14 down vote accepted

In wp-includes/l10n.php you will find the function get_locale(). It offers a filter; you can set the language and ignore the constant:

function get_locale() {
    global $locale;

    if ( isset( $locale ) )
        return apply_filters( 'locale', $locale );

    // WPLANG is defined in wp-config.
    if ( defined( 'WPLANG' ) )
        $locale = WPLANG;

    // If multisite, check options.
    if ( is_multisite() ) {
        // Don't check blog option when installing.
        if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) )
            $ms_locale = get_site_option('WPLANG');

        if ( $ms_locale !== false )
            $locale = $ms_locale;

    if ( empty( $locale ) )
        $locale = 'en_US';

    return apply_filters( 'locale', $locale );

To change it per plugin use the filter 'locale'. Example:

add_filter( 'locale', 'wpse_52419_change_language' );
function wpse_52419_change_language( $locale )
    return 'de_DE';
share|improve this answer
Works like a magic. But in my case, it's returning an error: Fatal error: Cannot redeclare get_locale(). So I simply changed the function name from get_locale() to nano_get_locale(), and it's working wonderfully. – Mayeenul Islam Nov 4 '13 at 9:25
@MayeenulIslam You should not copy the get_locale() function, that’s a quote from the core. All you need is the last code block. – toscho Nov 4 '13 at 12:38
great @toscho, many thanks. However, this does not work if I switch OPCache on on my local MAMPpro... is that like it is or is there a solution? – Raphael Jeger Apr 10 '15 at 14:37
@RaphaelJeger This cannot be affected by OPCache, only an output chache can do that. If you still have this problem, ask a new question. – toscho Apr 10 '15 at 23:21

Your Answer


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.