Setting a redirect cookie in wordpress

So the only part that is missing from your code is checking what page you are currently on. The is_page() function is a good way to get this context.

You could try it this way (I did not test it, only writeup out of my head to show the concept):

function set_newuser_cookie() {
    if (!isset($_COOKIE['subscriber']) && is_page('my-page-slug-page1')) {
        setcookie('subscriber', no, 0, COOKIEPATH, COOKIE_DOMAIN, false);
    }
}
add_action( 'init', 'set_newuser_cookie');

function my_cookie_redirect() {
   if (isset($_COOKIE['subscriber']) && is_page('my-page-slug-page2')) {
      wp_redirect('/page3');
      exit;
   }
}
add_action('template_redirect', 'my_cookie_redirect', 1);

The wordpress is_page() function either takes the page id, page slug or page_title as param.
http://codex.wordpress.org/Function_Reference/is_page

You should also always exit after an redirect, because otherwise the user would first load page-2 before beeing redirect to page-3

Leave a Comment