-2

I have a Wordpress page and I need to do the following:

  1. check if a cookie with the name petar_cookie exists before the page loads
  2. if no cookie, load the page as usual
  3. if yes cookie, redirect to a different URL

Is there a way to accomplish this in Wordpress?

I have found some plugins that allow you to enter PHP code into a page, but this doesn't help - I can't redirect from PHP once the page has started loading.

So, I need a way to insert PHP code before the <head> portion of the page and I need it to be able to read the $_COOKIE array at that point.

sveti petar
  • 3,626
  • 12
  • 58
  • 122
  • You can do something in PHP before headers are sent, see: https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php or https://stackoverflow.com/search?q=php+headers+sent – Luuk Feb 02 '21 at 12:20
  • @Luuk I know this in a basic PHP file, but how/where can I do this in Wordpress? – sveti petar Feb 02 '21 at 12:28
  • 1
    Asking us for recommendations on/help with finding any such plugins, would be considered off-topic here to begin with. If you want to implement this yourself, then the way to go would be to find one of the _hooks_ that run really early in the whole page building process, before any output has been created, and have code from your own plugin (or from inside the theme’s function.php) add its own function call to that. – misorude Feb 02 '21 at 12:46
  • @misorude I don't use Wordpress, I just inherited a landing page and I can't find a solution for this. I would think this is simple for a WP dev, but apparently I'm better off paying someone to do it than trying to get a straight answer out of this community. – sveti petar Feb 02 '21 at 13:04
  • 1
    I would ```hook``` my custom function that checks for the cookie, to ```after_setup_theme``` just because it happens early in the loading of wordpress right before cookie authentication – Ruvee Feb 02 '21 at 13:15
  • 1
    Does this answer your question? [How can I set, get and destroy cookies in WordPress?](https://stackoverflow.com/questions/6183162/how-can-i-set-get-and-destroy-cookies-in-wordpress) – disinfor Feb 02 '21 at 14:22
  • @disinfor: he does not use WordPress (see comment from him). If he really does not use Wordpress he should not have the need to do something "before page load in Wordpress" – Luuk Feb 02 '21 at 17:00
  • 1
    @Luuk read it again. OP is saying they've never used WP before, but they inherited a WP site so they need help. In the question _Is there a way to accomplish this in Wordpress?_ – disinfor Feb 02 '21 at 17:07
  • @disinfor: so, he does use Wordpress.... (sigh) Why is he saying he does not use it ? Now I am really confused.... – Luuk Feb 02 '21 at 17:10
  • 1
    OP is saying that they do not normally use WP. – disinfor Feb 02 '21 at 17:11
  • @disinfor Using the `$_COOKIE` variable is familiar to me, but I don't know which file to edit and where to check it. I've tried in the wp-settings file but `$_COOKIE` is `null` there. If I edit a page and `var_dump($_COOKIE)` there I see the cookie array, but then it's too late to redirect because headers have been sent. I' on the verge of doing something as hacky as echoing a Javascript line that does the redirect from the page just to get this over with. – sveti petar Feb 03 '21 at 08:09

1 Answers1

1

Put the following code on functions.php of the active theme:

function redirect_if_cookie() {
      if ( is_page('slug-of-your-page') && isset($_COOKIE['petar_cookie'])) {
          wp_redirect( home_url( '/new-url/' ) );
          die;
      }
}
add_action( 'template_redirect', 'redirect_if_cookie' );

https://developer.wordpress.org/reference/hooks/template_redirect/

Lucius
  • 1,292
  • 1
  • 7
  • 18
  • This works in checking the cookie. However, when it redirects to the software subdomain, the user isn't logged into the software now in that tab. But in another tab, the user is logged in. I don't know how this is possible. – sveti petar Feb 03 '21 at 08:25
  • The subdomain is different than the original site? – Lucius Feb 03 '21 at 15:07
  • Wordpress is at domain.com, the software is at software.domain.com – sveti petar Feb 04 '21 at 08:14