Title: Cannot set cookie when page cache was enabled.
Last modified: May 21, 2020

---

# Cannot set cookie when page cache was enabled.

 *  Resolved [vee](https://wordpress.org/support/users/okvee/)
 * (@okvee)
 * [5 years, 12 months ago](https://wordpress.org/support/topic/cannot-set-cookie-when-page-cache-was-enabled/)
 * I created this simple plugin to make sure that cookie can be set but when page
   cache was enabled, it is not working.
 *     ```
       <?php
       /**
        * Plugin Name: Test cookie with W3TC
        * Plugin URI:
        * Description:
        * Version:
        * Author: Vee W
        * Author URI: 
        * License: MIT
        * License URI: http://opensource.org/licenses/MIT
        * Text Domain: test-cookie-w3tc
        * Domain Path: /languages/
        * 
        * @package test-cookie-w3tc
        */
   
       add_action('init', 'testcookiew3tc');
   
       function testcookiew3tc()
       {
           setcookie('testcookiew3tc', date('Y-m-d H:i:s'), 0, COOKIEPATH, COOKIE_DOMAIN);
       }
       ```
   
 * The W3TC plugin settings is all default with page cache enabled.
    -  This topic was modified 5 years, 12 months ago by [vee](https://wordpress.org/support/users/okvee/).

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Contributor [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * (@vmarko)
 * [5 years, 12 months ago](https://wordpress.org/support/topic/cannot-set-cookie-when-page-cache-was-enabled/#post-12871310)
 * Hello [@okvee](https://wordpress.org/support/users/okvee/)
 * Thank you for your inquiry and I am happy to assist you with this.
    Does it work
   once the Page Cache is cleared? Thanks!
 *  Thread Starter [vee](https://wordpress.org/support/users/okvee/)
 * (@okvee)
 * [5 years, 12 months ago](https://wordpress.org/support/topic/cannot-set-cookie-when-page-cache-was-enabled/#post-12871442)
 * Yes, it is.
 *  Thread Starter [vee](https://wordpress.org/support/users/okvee/)
 * (@okvee)
 * [5 years, 12 months ago](https://wordpress.org/support/topic/cannot-set-cookie-when-page-cache-was-enabled/#post-12873403)
 * I see this document ( [https://github.com/W3EDGE/w3-total-cache/wiki/FAQ:-Usage#which-textareas-for-file-entries-support-regular-expressions](https://github.com/W3EDGE/w3-total-cache/wiki/FAQ:-Usage#which-textareas-for-file-entries-support-regular-expressions))
   said.
 * > To disable page caching of specific theme templates or plugin files you need
   > to add `define('DONOTCACHEPAGE', true);` to it.
 * But it is incorrect. With that constant, it will be affect all other plugins 
   or maybe the whole site not just specific theme or plugin.
 * I can’t make my plugin working properly while page cache is enabled or not cleared
   the cache.
 *  Plugin Contributor [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * (@vmarko)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/cannot-set-cookie-when-page-cache-was-enabled/#post-12874482)
 * Hello [@okvee](https://wordpress.org/support/users/okvee/)
 * Thank you for the information. The define(‘DONOTCACHEPAGE’, true); constant is
   disabling page caching on a specific template.
    So you can also use something
   like:
 *     ```
       if ( ! defined( 'DONOTCACHEPAGE' ) )
         define( 'DONOTCACHEPAGE', true );
       ```
   
 * There is a filed in Pefrormance>Page Cache>Advanced>Rejected cookies: to Never
   cache pages that use the specified cookies.
    I hope this helps!
    -  This reply was modified 5 years, 11 months ago by [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/).
 *  Thread Starter [vee](https://wordpress.org/support/users/okvee/)
 * (@okvee)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/cannot-set-cookie-when-page-cache-was-enabled/#post-12874517)
 * No, that constant once defined it will be affected all other plugins.
    I just
   want only this plugin (my plugin) to working without cache because it must be
   set the cookie.
 * I tried rejected cookies but that is not what how it should work. That reject
   cookies will work on skip caching but I want my plugin to set cookie every request
   whether there is reject cookie or not.
    For example: 1. Guests enter the web 
   page. My plugin should work and set cookie. 2. Logged in users enter the web 
   page. My plugin should work and set cookie.
 * But currently it is:
    1. Guests enter the web page. My plugin work if cache was
   cleared or page cache was disabled, otherwise it will not working. So, reject
   cookie can’t help with this.
    -  This reply was modified 5 years, 11 months ago by [vee](https://wordpress.org/support/users/okvee/).
 *  Plugin Contributor [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * (@vmarko)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/cannot-set-cookie-when-page-cache-was-enabled/#post-12876961)
 * Hello [@okvee](https://wordpress.org/support/users/okvee/)
 * Thank you for your patience.
    I’ve spoken with the developers and the problem
   is that, as you already know PHP is not executed. that’s what caching does and
   why it’s effective so giving that the cache is active PHP is not fired. The only
   solution for this is to use ajax calls. Thank you!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Cannot set cookie when page cache was enabled.’ is closed to new replies.

 * ![](https://ps.w.org/w3-total-cache/assets/icon-256x256.png?rev=1041806)
 * [W3 Total Cache](https://wordpress.org/plugins/w3-total-cache/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/w3-total-cache/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/w3-total-cache/)
 * [Active Topics](https://wordpress.org/support/plugin/w3-total-cache/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/w3-total-cache/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/w3-total-cache/reviews/)

## Tags

 * [cookies](https://wordpress.org/support/topic-tag/cookies/)

 * 6 replies
 * 2 participants
 * Last reply from: [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * Last activity: [5 years, 11 months ago](https://wordpress.org/support/topic/cannot-set-cookie-when-page-cache-was-enabled/#post-12876961)
 * Status: resolved