Alex
Alex

Reputation: 111

Wordpress 4: WPLANG deprecated. How to change language programmatically?

Since WPLANG is deprecated in Wordpress 4, what do you use to set a user selected language? In versions 3.x.x I used define('WPLANG', $lang) to set a language and then on the pages could get it using get_locale(). I need to use this approach to differentiate the content for the different languages. I know that it's possible to change the language in Settings->General but I need to do that programmatically.

Thanks

Upvotes: 4

Views: 10432

Answers (3)

David
David

Reputation: 813

Instead of juggling with global variables or constants one could use the filter locale to adapt the value on the fly. That would also be more fail-save for future releases.

add_filter( 'locale', function( $default_locale ) {
    if ( isset( $_SESSION[ 'WPLANG' ] ) )
        return $_SESSION[ 'WPLANG' ];

    return $default_locale;
} );

By the way, WPLANG as key in the session is likely at risk to cause a naming collision issue. Keep in mind that other WordPress plugins may also make usage of the global session.

Upvotes: 1

Alex
Alex

Reputation: 111

I found a solution that works for me. Instead of using define ('WPLANG', $_SESSION['WPLANG']); I use $locale = $_SESSION['WPLANG']; .

Upvotes: 2

Bjoern
Bjoern

Reputation: 16304

With WordPress 4.0 the define WPLANG from wp-config.php is - as you have mentioned - depreciated. It has been replaced by an option called WPLANG stored in the table <TablePrefix>_options.

You could use get_option() to access it:

$my = get_option('WPLANG','en_US');

More details about the change can be found here.

Upvotes: 6

Related Questions