WordPress setcookie() on splash page

I’m having a WordPress website, and on main page, if visitor is here for the first time, a ‘Subscribe to newsletter’ modal appears. I’m having a problem that cookie is not being set via setcookie() function:

<?php 
            if($_COOKIE['rcngVisited'] != 'true')
            {
                echo do_shortcode('[modal name="Subscribe to newsletter" style=button color=default size=default][/modal]'); 
                setcookie('rcngVisited','true', time()+60*60*24*30, '/readyclickandgo/');
            }
        ?>

Related posts

Leave a Reply

1 comment

  1. You only set the cookie for readyclickandgo dir

    Make sure you don’t have any output before you set COOKIE, add this in your header:

    <?php
        $visit = false;
        if($_COOKIE['rcngVisited'] != 'true'){
            setcookie('rcngVisited','true', time()+60*60*24*30, '/readyclickandgo/');
            $visit = true;
        }
    ?>
    

    Add this anywhere you want in same page:

    <?php
        if($visit){
            echo do_shortcode('[modal name="Subscribe to newsletter" style=button color=default size=default][/modal]'); 
        }
    ?>