Wordpress 4: WPLANG is deprecated. How do I change the language programmatically?

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

thank

+3


source to share


3 answers


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



+1


source


In WordPress 4.0, the definition WPLANG

from wp-config.php

- as you mentioned - is depreciated. It has been replaced with the option WPLANG

stored in the table <TablePrefix>_options

.

You can use get_option()

to access it:



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

      

More information on the change can be found here .

0


source


Instead of manipulating global variables or constants, you can use a filter locale

to adapt the value on the fly. It will also be more unfortunate for future releases.

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

    return $default_locale;
} );

      

By the way, WPLANG

as a key in the session, it probably runs the risk of causing a naming collision problem. Be aware that other WordPress plugins can use the global session as well.

0


source







All Articles