How to set a cookie in Wordpress How to set a cookie in Wordpress wordpress wordpress

How to set a cookie in Wordpress


You have to set them before anything is outputted

look there: How can I set, get and destroy cookies in Wordpress?

If you are using a theme in function.php

function set_new_cookie() {    //setting your cookies there}add_action( 'init', 'set_new_cookie');

Your expiration date is 0 so you cookies will be deleted right away look at the php doc:

EDIT:From php.net:

If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

http://php.net/manual/en/function.setcookie.php

You have to set it like this for example :

setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */


  1. Setting a Cookie:The below example will set the cookie that expired for one hour (60*60 seconds) since it set with COOKIEPATH and COOKIE_DOMAIN was defined by WordPress according to your site path and domain.

    setcookie( 'my-cookie-name', 'my-cookie-value', time() + 3600, COOKIEPATH, COOKIE_DOMAIN );
  2. Getting a Cookie:Getting a cookie can be done by using variable $_COOKIE which contains an associative array.

    $myCookie = isset( $_COOKIE['my-cookie-name'] ) ? $_COOKIE['my-cookie-name'] : 'Not Set!!';
  3. Delete or Unset a Cookie: It is same as the above instruction #1, just with a negative time to expire the cookie;

    setcookie( 'my-cookie-name', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN );