Multilingual theme: Changing locale based on URL

My problem here is that get_query_var('language') isn’t defined in the function set_my_locale

Because get_locale (which applies the filter locale) is called before wp() (which parses the query & sets up the variables).

You’ll have to manually inspect the request yourself:

if ( strpos( $_SERVER['REQUEST_URI'], '/de/' ) === 0 ) {
    // German
} else {
    // English
}

Note this expects WordPress to be running under the root URL i.e. http://example.com/

Leave a Comment