0

I have set a cookie in my functions.php as follows:

add_action('send_headers', 'sfdc_cookie');
function sfdc_cookie() {
    if( isset($_GET['sfdc']) && '' != $_GET['sfdc'] ) {
        if( empty($_COOKIE['sfdc']) ){
            if ($_GET['sfdc'] == 'mim'):
            setcookie('sfdc', 'cookievalue', time()+3156000, '/', 'www.mydomain.com' );
            //$_COOKIE['sfdc'] = 'cookie';
            endif;
        }                          
    }
    echo $_COOKIE['sfdc'];
}

if I access any page on the site with ?sfdc=mim, the value is shown. I assumed the cookie is set at that point, and when I check the stored cookies from the Chrome browser, it appears to be set. However, if the querystring is absent, it is as if the cookie does not exist. I cannot even echo the cookie onto the page.. Why can't I echo the value when the querystring is not present in the URL?

I have changed my add_action from send_headers to init with the same result.

osakagreg
  • 537
  • 4
  • 19
  • maybe you want to use `$_SESSION` variable? it works for me rly good. You can also check if `session` is already started and if not, you can start it and then you check for session variables. is this helpful? – Jon Nezbit Sep 09 '20 at 22:18

1 Answers1

0

You should fire up this function like this:

add_action( 'wp_enqueue_scripts', 'sfdc_cookie' ); // <-- this starts the function with the same name in the location named 'wp_enqueue_scripts' 
function sfdc_cookie() {
if( isset($_GET['sfdc']) && '' != $_GET['sfdc'] ) {
    if( empty($_COOKIE['sfdc']) ){
        if ($_GET['sfdc'] == 'mim')
        {
        setcookie('sfdc', 'cookievalue', time()+3156000, '/', 'www.mydomain.com' );
        //$_COOKIE['sfdc'] = 'cookie';
        }
    }                          
}

if you want start cookie on page and make it available in others, you should do it like this:

add_action( 'init', 'sfdc_cookie' );
function sfdc_cookie() {
   if( !isset($_COOKIE['sfdc']) ) {
      setcookie('sfdc', 'cookievalue', time()+3156000, '/', 'www.mydomain.com' );
   }
}
Kafus
  • 31
  • 5
  • never seen `endif` in php. Is it some `wordpress` specific thing or what? – Jon Nezbit Sep 09 '20 at 22:19
  • @JonNezbit yes, you can use it in WP or in Laravel but not this way. – Kafus Sep 09 '20 at 22:21
  • 1
    endif is used in PHP all the time. https://www.php.net/manual/en/control-structures.alternative-syntax.php – osakagreg Sep 09 '20 at 22:25
  • @Kafus your solution did not work. It does not appear to set the cookie at all when I use wp_enqueue_scripts as opposed to send_headers. I cleared the cookie before testing and it would not set or echo. – osakagreg Sep 09 '20 at 22:27
  • @osakagreg try to use it in `init` not in `wp_enqueue_scripts` - location `init` is on every wordpress page, so it will be: add_action( 'init', 'sfdc_cookie' ). – Kafus Sep 09 '20 at 22:32
  • @Kafus I'm having the same issue using init. It is available only on the page from which it is set. I see the cookie in my Chrome settings, but it is not be read on other pages. – osakagreg Sep 09 '20 at 22:37
  • @osakagreg so you want this cookie in all pages? If so, you have bad `if`s. I'll change it for you – Kafus Sep 09 '20 at 22:43
  • @osakagreg I eddited my Asnwer. You can add `if( isset($_GET['sfdc'])` if you want. But code `'' != $_GET['sfdc']` is quite strange. it means: if `nothing` is not equal `$_GET['sfdc'] – Kafus Sep 09 '20 at 22:49
  • @Kafus that's just making sure it's set and has a value. Anyhow, I tried and it still does not work beyond one page. You left a closing parenthesis in your code: if( !isset($_COOKIE['sfdc'] ) { – osakagreg Sep 09 '20 at 22:52
  • @osakagreg "that's just making sure it's set and has a value" - if you wanna do this it should be: `isset($_GET['sfdc']) && $_GET['sfdc'] != '' `. About question: if you set `domain`- try to remove `www` or even try to remove last two parameters: `path` and `domain` – Kafus Sep 09 '20 at 23:03
  • @Kafus I think it's standard. https://stackoverflow.com/questions/10849667/php-reversed-order-in-if-statement. Anyhow, I have tried changing the domain values. Nothing works. Maybe there is a plugin conflict or something else weird going on. I'm bending over backwards just to get a cookie value. I'll try this on a different installation of WP that has no plugins and see what happens. – osakagreg Sep 09 '20 at 23:08