I want to write referral URL to cookie and use it at some point to echo that referral URL on my website. What is the best way to achieve this?

Thank you

  • Generic PHP question in this form. If it has some WP aspects I am missing please elaborate and edit. – Rarst Oct 28 '14 at 17:56
  • I was thinking that it could be some WP function, but now I see that it is just PHP thing. Thanks. – MIC Oct 30 '14 at 9:03

You can do this as we do in PHP. You can set cookie following way.

setcookie( 'cookie-name', 'cookie-value', 'cookie-expiration-time', 'cookie-path', 'cookie-domain', false );

For referral url, I made following code that you can use to set a cookie with name my-referral-cookie and time() + 3600 (1 hour from current time) is expiration time for cookie.

setcookie( 'my-referral-cookie', $_SERVER['HTTP_REFERER'], time() + 3600, COOKIEPATH, COOKIE_DOMAIN, false );

You can get the value of cookie like this.

echo $_COOKIE['my-referral-cookie'];

That's it.

| improve this answer | |
  • Thanks :) I was thinking that where could be some WP function or something. Quite simple :) thank you for your help! – MIC Oct 30 '14 at 9:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.