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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.phpfor example), egRetrieving cookies in PHP is much easier. Simply get them by name from the
$_COOKIEsuper 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