WordPress.org

Support

Support » Themes and Templates » Place a cookie when one specific page has been visited?

Place a cookie when one specific page has been visited?

  • How can I add a cookie when one specific page has been visited?

    I want to redirect new visitors to a questionnaire. When they submit the questionnaire they’re redirected to a thank you page. I want the thank you page to place a cookie.

    I’d like to know the best way to achieve this with WP. Currently I’ve got the following in my functions.php, but it doesn’t isolate the thank you page:

    function set_newuser_cookie() {
        if (!isset($_COOKIE['new_visitor'])) {
            setcookie('new_visitor', 1, time()+3600*24*100, '/', '.mydomain.com/thank-you/', false);
        }
    }
    add_action( 'init', 'set_newuser_cookie');
Viewing 2 replies - 1 through 2 (of 2 total)
  • I have not done this before, but you could probably use: <?php is_page($page); ?> to add logic.

    Thanks for the response.

    I tried this first but couldn’t get it to work. The cookie has to be set via functions.php and then needs to be checked for on the first line of the header.php file before any other content loads. WP tags like is_page() don’t work before the page loads.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Place a cookie when one specific page has been visited?’ is closed to new replies.
Skip to toolbar