0

I have this custom pligin that im using in order to save the user's language preferences. I store it as a cookie like so:

<?php
    
    add_filter( 'init', 'aws_set_language_cookie', 999 );
    function aws_set_language_cookie() {
        global $TRP_LANGUAGE;
        if ( isset( $TRP_LANGUAGE ) ) {
            $result = setcookie( 'language', $TRP_LANGUAGE, time() + ( 30 * DAY_IN_SECONDS ), "/", "" , true, true );
        }
    }
    ?>

Now if you take a look at the website https://ugrandchurch.com/ open cookies in inspect element after switching to Spanish. You will see that a cookie by the name of "language" created which holds the value of "es_ES" is saved but it changes to "en_EN" after 3 seconds.

How can I make sure that the value of Spanish "es_ES" is kept instead of being taken overriten by "en_EN".

The WordPress language plugin used here is by the name of TranslatePress

3
  • In my browser "es_ES" doesn't change to "en_EN" after 3 seconds. Why do you need to set cookie by yourself. I guess TranslatePress plugin should store users language somewhere in session
    – anton
    Commented May 19, 2021 at 15:09
  • @anton thats my bad im sleep deprived i posted the question and started working on different solutions i was changing the cookie files now its the original you can see what i mean
    – user206592
    Commented May 19, 2021 at 15:15
  • Guess problem is in other function. this filter doesn't fire on your language select change.
    – anton
    Commented May 20, 2021 at 7:44

1 Answer 1

0

I didn't see this happening on your site, but I think you could probably remove the variable declaration in front of setcookie(). I didn't see result called used any where in this code. Which makes me wonder if the value is being set because since the variable isn't used.

<?php
    
    add_filter( 'init', 'aws_set_language_cookie', 999 );
    function aws_set_language_cookie() {
        global $TRP_LANGUAGE;
        if ( isset( $TRP_LANGUAGE ) ) {
            setcookie( 'language', $TRP_LANGUAGE, time() + ( 30 * DAY_IN_SECONDS ), "/", "" , true, true );
        }
    }
    ?>

I hope this helps, sorry if it doesn't :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.