3

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

Alex
  • 101
  • 1
  • 5

3 Answers3

5

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.

Bjoern
  • 15,934
  • 4
  • 43
  • 48
  • The code that I used before checked cookies and got language from there, then this language was set in WPLANG and on the pages I used get_locale() to get it. For now I can get a cookie but how to set it for the current user? Should I use update_option()? – Alex Dec 09 '14 at 01:59
  • Sorry I forgot to mention that checking cookies and setting WPLANG is done using wp-config.php (require_once(dirname(__FILE__).'/set-lang.php');). So in this case I think I cannot use update_option()... – Alex Dec 09 '14 at 02:10
  • Question: Is your user logged in into your WordPress based website, or is user meant as everyone who watches that website? – Bjoern Dec 09 '14 at 05:39
  • Everyone who watches the website. – Alex Dec 09 '14 at 22:27
  • In that case use a cookie. Updating the option with `update_option()` everytime a user visits the page would stress your database table quite significantly... – Bjoern Dec 10 '14 at 18:39
2

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

Alex
  • 101
  • 1
  • 5
0

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.

David
  • 803
  • 6
  • 13