$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Setting a redirect cookie in wordpress|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Setting a redirect cookie in wordpress

matteradmin9PV0评论

I've been searching on the net for a solution to my problem all day, but I can't make sense of anything I see, since I know absolutely NOTHING about coding.

What I'd like to do:

  1. I'd like to set a cookie when a person visits one particular page on my website, let's call it Page 1.
  2. Then, when that visitor goes to another unrelated page (Page 2), he/she gets automatically redirected to Page 3 (because of the cookie that was set when they visited Page 1). Visitors without the cookie, don't ever get to see Page 3.

What I've managed to do so far:

I've managed to SET the cookie in Wordpress functions.php, by inserting the following:

function set_newuser_cookie() {
    if (!isset($_COOKIE['subscriber'])) {
        setcookie('subscriber', no, 0, COOKIEPATH, COOKIE_DOMAIN, false);
    }
}
add_action( 'init', 'set_newuser_cookie');

I've managed to define a redirect by inserting the following in functions.php:

if (!isset ($_COOKIE['subscriber']))
header ("Location: page2");
else
header ("Location: page3");

That's as far as I've gotten. It doesn't work because neither of the above are page-specific. I only want the cookie to be set (or variable changed) when the visitor visits one specific page, and then get redirected when he visits another specific page.

Is this doable?

Oh, and by the way, I'm on a self-hosted Wordpress site.

Thanks so much.

Sammie

I've been searching on the net for a solution to my problem all day, but I can't make sense of anything I see, since I know absolutely NOTHING about coding.

What I'd like to do:

  1. I'd like to set a cookie when a person visits one particular page on my website, let's call it Page 1.
  2. Then, when that visitor goes to another unrelated page (Page 2), he/she gets automatically redirected to Page 3 (because of the cookie that was set when they visited Page 1). Visitors without the cookie, don't ever get to see Page 3.

What I've managed to do so far:

I've managed to SET the cookie in Wordpress functions.php, by inserting the following:

function set_newuser_cookie() {
    if (!isset($_COOKIE['subscriber'])) {
        setcookie('subscriber', no, 0, COOKIEPATH, COOKIE_DOMAIN, false);
    }
}
add_action( 'init', 'set_newuser_cookie');

I've managed to define a redirect by inserting the following in functions.php:

if (!isset ($_COOKIE['subscriber']))
header ("Location: page2");
else
header ("Location: page3");

That's as far as I've gotten. It doesn't work because neither of the above are page-specific. I only want the cookie to be set (or variable changed) when the visitor visits one specific page, and then get redirected when he visits another specific page.

Is this doable?

Oh, and by the way, I'm on a self-hosted Wordpress site.

Thanks so much.

Sammie

Share Improve this question edited Apr 26, 2013 at 9:21 Mike Madern 3,9593 gold badges25 silver badges33 bronze badges asked Apr 26, 2013 at 8:10 SammieSammie 612 silver badges5 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

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/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

That did it s_ha_dum! You didn't quite get what I wanted, that's my fault for not having been able to explain it properly. I didn't want the visitor to get redirected to the home page at any point.

Below is what I settled with: (edited on 27th apr 2013)

I'm still having problems though.

Trouble is, it works flawlessly on my mac (chrome, safari and firefox), but doesn't on my PC. On the PC, it sets the cookies just fine, but the redirect happens regardless of whether the cookie is set or not.

New edit: Got it working now! Thanks all. Code below works flawlessly.

function set_newuser_cookie() {
   global $post;
   if (!isset($post->ID)) return;

 if ($post->ID == 1 || $post->ID == 7 || $post->ID == 8 || $post->ID == 9) 

 {
     if (!isset($_COOKIE['subscriber'])) {
        setcookie('subscriber', no, 0, COOKIEPATH, COOKIE_DOMAIN, false);
     }          
   }

   if (isset($_COOKIE['subscriber']) && $post->ID == 2 || $post->ID == 10) {
     wp_safe_redirect('/page3/');
     exit;
   }


}
add_filter('template_redirect','set_newuser_cookie',1);
function template_redir_wpse_97315() {
   global $post;
   if (!isset($post->ID)) return;

   if ($post->ID == 1) {
     if (!isset($_COOKIE['subscriber'])) {
        setcookie('subscriber', no, 0, COOKIEPATH, COOKIE_DOMAIN, false);
     }          
   }

   if (is_single() && $post->ID == 2 && isset($_COOKIE['subscriber'])) {
     wp_safe_redirect(home_url('/page-3/'));
     exit;
   }
   
   if (is_single() && $post->ID == 2 && !isset ($_COOKIE['subscriber'])) {
     wp_safe_redirect(home_url());
     exit;
   }
}
add_filter('template_redirect','template_redir_wpse_97315',1);

I admit that I am not quite sure if the logic is right. You description of the redirects plus the code you posted has me a bit confused. However, that should be the basic idea.

The post IDs are obviously wrong. You will need to sort those out. get_permalink(<post-id>) should do it. You may need other conditions to make those redirects behave.

Reference

http://codex.wordpress/Plugin_API/Action_Reference/template_redirect
http://codex.wordpress/Function_Reference/wp_safe_redirect http://codex.wordpress/Function_Reference/home_url

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far