Including a wp-lang file to define WPLANG according to user’s choice (WordPress)?

In this previous questions, someone told me how to make WPLANG clickleable, so that the user can choose his preferred language (usually you can only define WPLANG by modifying the wp-config). But I’m sure how to make the link. Are the following functions giving me the possibility of using example/?lang= Or example/en?

*wp-lang.php

Read More
 session_start();
 if ( isset( $_GET['lang'] ) ) {
    $_SESSION['WPLANG'] = $_GET['lang'];
    define ('WPLANG', $_SESSION[WPLANG]);
 } else {
    if(isset($_SESSION['WPLANG'])) {
        define ('WPLANG', $_SESSION['WPLANG']);
        $_GET['lang'] = $_SESSION['WPLANG'];
    } else {
        if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ) {
            $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
             $languages = explode( ",", $languages );
            $_SESSION['WPLANG'] = $languages[0];
            $_SESSION['WPLANG'] = str_replace("-", "_", $_SESSION['WPLANG']);
            $_GET['lang'] = substr($_SESSION['WPLANG'],0,2);
            define ('WPLANG', $_SESSION[WPLANG]);
        } else {
            define ('WPLANG', '');
        }
    }
 }

*wp-config.php – Find the section where the constant WPLANG is defined. Add in the following line just before the WPLANG declaration.

 require_once(dirname(__FILE__).'/wp-lang.php');
 define ('WPLANG', ''); 

Related posts

Leave a Reply

1 comment

  1. I was modifying WP_LANG conditionally directly in wp-config.php using $_GET['lang'] and http://example.com/wp-admin/lang=es_ES.

    In this example (used in wp-config.php), I’ve added a cookie, so once you go to any admin link ?lang=lang_COUNTRY, the subsequent loads will be on the new language.

    $lang = '';
    if( isset( $_GET['lang'] ) )
    {
        $lang = $_GET['lang'];
        setcookie( 'my_lang', $lang, time() + 3600 * 24 * 365, '/', '.example.com' );
    } 
    elseif ( isset( $_COOKIE['my_lang'] ) )
    {
        $lang = $_COOKIE['my_lang'];
    }   
    define( 'WPLANG', $lang );