Search code examples
phpwordpressconstantsqtranslate

cant access constant - define() - within functions.php in Wordpress . accessed gives the wrong result


In wp-config.php I defined WPLANG by:

if(isset($_GET['lang']) && $_GET['lang'] == 'pl') {
  define('WPLANG', 'pl_PL');
} else {
  define('WPLANG', 'en_US');
}

whenever I access the WPLANG it works perfectly, however when I try to add metadata from a mailpress widget it fails to recognize the WPLANG. The function for inserting the language to wpdb is located in functions.php of the theme:

add_action( 'MailPress_insert_user', 'MailPress_insert_set_lang');
    function MailPress_insert_set_lang($mp_user_id) {
        global $wpdb;
        $q = $wpdb->prepare( "INSERT INTO $wpdb->mp_usermeta 
        (mp_user_id, meta_key, meta_value) 
        VALUES (%s, 'lang', %s)", $mp_user_id, WPLANG );
        $wpdb->query( $q );
    }

the Mailpress widget uses ajax. Whenever i put echo WPLANG before the function definition it echoes the right language, however WPLANG is not recognized within the scope of that function.

Please advise.


Solution

  • Possibly try wrapping WPLANG with constant()

    add_action( 'MailPress_insert_user', 'MailPress_insert_set_lang');
    function MailPress_insert_set_lang($mp_user_id) {
        global $wpdb;
        $q = $wpdb->prepare( "INSERT INTO $wpdb->mp_usermeta 
        (mp_user_id, meta_key, meta_value) 
        VALUES (%s, 'lang', %s)", $mp_user_id, constant(WPLANG) );
        $wpdb->query( $q );
    }