WordPress.org

Ready to get started?Download WordPress

Forums

Place a cookie when one specific page has been visited? (3 posts)

  1. w1cky
    Member
    Posted 2 years ago #

    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');
  2. potentweb
    Member
    Posted 2 years ago #

    I have not done this before, but you could probably use: <?php is_page($page); ?> to add logic.

  3. w1cky
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic