How can I set, get and destroy cookies in Wordpress?
I surfed the web but I can't get clear ideas, please help me find how.
How can I set, get and destroy cookies in Wordpress?
I surfed the web but I can't get clear ideas, please help me find how.
You can either retrieve and manipulate cookies on the server side using PHP or client side, using JavaScript.
In PHP, you set cookies using
setcookie()
. Note that this must be done before any output is sent to the browser which can be quite the challenge in Wordpress. You're pretty much limited to some of the early running hooks which you can set via a plugin or theme file (functions.php
for example), egRetrieving cookies in PHP is much easier. Simply get them by name from the
$_COOKIE
super global, egUnsetting a cookie requires setting one with an expiration date in the past, something like
For JavaScript, I'd recommend having a look at one of the jQuery cookie plugins (seeing as jQuery is already part of Wordpress). Try http://plugins.jquery.com/project/Cookie
Try this code inside function.php to play with Cookies in wordpress
Set a Cookie in wordpress
Get a Cookie in wordpress
Delete or Unset a Cookie in wordpress