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

I wonder, is there a way to change the locale at runtime using WordPress ?

What I mean, is, I have create a custom URL in order to create an XML file with data for integration with another web application. The URL has also a language portion. Lets say this is my URL


where last portion of the URL is the english language. In that case what I like is the WordPress to generate an English XML. But how can I instruct the WordPress to change the locale in that point of the execution ?

Note that I have to change the locale because I use also system variables that are translated, such us __(), _e() and so on.

share|improve this question
possible duplicate of Change language by clicking a button – kaiser Apr 18 '12 at 23:29

I'm trying to do a similiar thing, and the experts on the wp-hackers mailing list (Otto, Nacin) told me this:

Don't try to change WPLANG, you can't change a define'd constant. Instead, change the global $locale, or put a filter on 'locale'.

So the best solution is to apply a filter on the 'locale' global variable. The only way to do that is by creating a custom plugin. If you put the following piece of code into your functions.php file, it won't work properly because it will run too late in the WP loading sequence.

Your plugin could look like this (I'm just reusing the URI testing part from OneTrickPony, you can replace it with another conditional testing method):

Plugin Name: Change locale at runtime
Plugin URI: http://wordpress.stackexchange.com/questions/49451/change-locale-at-runtime

function wpsx_redefine_locale($locale) {
    // run your tests on the URI
        $lang = explode('/', $_SERVER['REQUEST_URI']);
        // here change to english if requested
        if(array_pop($lang) === 'en'){
          $locale = 'en_US';
        // otherwise stick to your default language
          $locale = 'gr_GR';
    return $locale;

I hope this can help anyone!

A few more warnings (quoting Andrew Nacin), regarding the cost in terms of performance when switching the locale:

It is possible to "switch out" a locale after the default locale is loaded, but I would advise against that as it is inefficient, unless your default language is English, in which case it is not so bad.

Default textdomain files are loaded after plugins_loaded and setup_theme, but before the theme is loaded and before after_setup_theme fires. Loading English then re-loading the textdomain into German on the init hook would be fine, performance-wise, as English has no mo files. But loading Spanish by default then switching to German would not.

See http://codex.wordpress.org/Plugin_API/Action_Reference for useful info about the loading sequence.

share|improve this answer

I don't know if it's possible to do this within a plugin, because of the constants WP requires defined before a certain point, but check for the requested language in wp-config.php and define the necessary constant:

// split URI
$lang = explode('/', $_SERVER['REQUEST_URI']);

// here change to english if requested
if(array_pop($lang) === 'en'){
  define('WPLANG', 'en_US');

// otherwise stick to your default language
  define('WPLANG', 'gr_GR');
share|improve this answer
+1 And, yes, it is possible to redefine the WPLANG constant from within a plugin. – Johannes Pille Apr 18 '12 at 17:33
Did you try adding in wp-config? In any case, check if you're defining WPLANG there – onetrickpony Apr 18 '12 at 17:40
@MerianosNikos: WPLANG should be languagecode_COUNTRYCODE, i.e. "en_US", "de_DE", "pt_BR" or the like. Other than that the above ought to work. – Johannes Pille Apr 18 '12 at 17:46
Do you a URI check for English only? – onetrickpony Apr 18 '12 at 18:00
@Merianos: See this pastebin for a simple plugin that should do the trick. – Johannes Pille Apr 18 '12 at 18:12

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.