• Resolved vee

    (@okvee)


    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: https://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 4 years, 5 months ago by vee.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @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

    (@okvee)

    Yes, it is.

    Thread Starter vee

    (@okvee)

    I see this document ( 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

    (@vmarko)

    Hello @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!

    Thread Starter vee

    (@okvee)

    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 4 years, 5 months ago by vee.
    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @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.