Warning: Redis::connect(): connect() failed: Connection refused in /var/www/com.justinsilver/www/html/wp-content/plugins/nginx-helper/includes/redis-delete.php on line 15
W3 Total Cache Fragment Caching in WordPress - Justin Silver

W3 Total Cache Fragment Caching in WordPress

W3 Total Cache, also known as W3TC, is a very powerful caching plugin for WordPress which is notoriously slow without tuning. For guest users much of your content will typically be static which makes serving up cached content to these users a pretty good idea. But what if you want part of the page to be be dynamic while most of the page is cached? Fragment caching to the rescue.

Configure W3 Total Cache

First you will need to make sure W3TC is properly configured for fragment caching. To enable it, from your WordPress Admin, visit Performance > General Settings and look under the Page Cache section.  Make sure that the Page cache method is set to Disk: Basic and save your settings.

Next visit Performance > Page Cache and scroll down to the Advanced section. Here you will need to check the box next to Late initialization to enable it. Notice the help text under it:

Enables support for WordPress functionality in fragment caching for the page caching engine. Use of this feature may increase response times.

Set W3TC_DYNAMIC_SECURITY Constant

For security reasons W3TC requires that you create a constant named W3TC_DYNAMIC_SECURITY and pass it to the sections of code you would like it to ignore (and execute!). An obvious choice for this is wp-config.php, but you might also have success with /wp-content/mu-plugins, a custom plugin, your theme’s functions.php, or even inline before your fragment – though the latter isn’t really recommended. You can set this to a hardcoded value, or use something random for each request.

define( 'W3TC_DYNAMIC_SECURITY', md5( rand( 0, 999999 ) ) );

Update your template

The final step is to update your template files to indicate where you want W3TC to *not* cache the page. This is done by using an HTML comment called `mfunc`. You pass it your W3TC_DYNAMIC_SECURITY and include any PHP you want to execute (without <?php> tags!).

<!--mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> -->
echo 'The time is '.date( 'H:i:s', time() );
<!--/mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> -->

You may also like...

28 Responses

  1. Tuan Tran says:

    Thanks for share, i was try but it don’t work with me. It just plain text and i don’t know what to do.

    I was add ‘mfunc’ and define( ‘W3TC_DYNAMIC_SECURITY’, md5( rand( 0, 999999 ) ) );

  2. Tuan says:

    Thank for post, but the last thing, where i can post that “mfunc” in my wordpress website ?

  3. Adrian says:

    I read elsewhere that you need to add mfunc to the “Ignored comment stems:” in the HTML & XML section of “Minify” settings.

  4. Lina says:

    Unfortunately its not working for me either, it always shows the error: “unable to execute code:” and then repeats the code within mfunc function. Before giving you details, I just wanted to know if fragment caching is only available for Pro Version of W3TC? Because I have only the free version…Many thanks in advance and happy easter 🙂

    • Justin Silver says:

      Hi Lina, I was using this with the free version of W3TC, but unfortunately I’m not sure why it wouldn’t be working for you. Good luck!

  5. Ramesh says:

    Hi Justin,

    I added define( ‘DONOTCACHEPAGE’, true ); in wp-config, and set W3TC, it working I think.

    • Justin Silver says:

      If you set that constant in wp-config.php it will disable caching on *all* pages, not just a specific template. If you want to just disable it for pages, for example, you would put it in page.php in your theme.

  6. Ramesh says:

    I just want to disable cache for specific php files, how can I do?

    • Justin Silver says:

      Hi Ramesh,

      The article above is for excluding certain parts of a page from the cache, but if you would like to exclude an entire page you can do it from the Page Cache settings (in the admin) by adding its path (or partial path using * as a wildcard. If you want to exclude a page template from ever being cached, you need to add the following constant declaration to the template:

      define( 'DONOTCACHEPAGE', true );
      

      Good luck!

  7. John says:

    very useful article
    thanks

  8. mark says:

    Hi,

    Will it work with Clouflare?

    Is there also a way to cache different pages for each country?

    Thanks

    • Justin Silver says:

      This code is specifically designed to work with the W3 Total Cache plugin (W3TC) so I’m not sure how it would work in conjunction with CloudFlare. If you are using CloudFlare exclusively then this wouldn’t have any effect, and unfortunately I’m not familiar enough to make any suggestions.

      If you used something like WordPress Multi Language (WPML) then you should be able to cache different language pages using W3TC. Should also work with custom solutions.

      • mark says:

        I actually have both, Cloudflare and W3TC. I prefer not to install more plugins. Maybe I can define cloudflare not to cache and then follow your solution. Think this way it will work?

        • Justin Silver says:

          Like I mentioned I’m not that familiar with CloudFlare, but that certainly sounds reasonable. If you can tell CloudFlare to not cache a page then it will fall through to your server. Assuming W3TC is properly configured it should serve pages from your cache, and in that case the fragment caching would be supported as I included in my blog post.

  9. nev says:

    This does NOT work for me.

    • Justin Silver says:

      Sorry to hear that… it worked for me in my testing. Is it just caching the entire page? Not caching at all? Any errors?

      • JackTheKnife says:

        Same issue for me. It shows once then is cached as a plain text not excluded from caching. Works fine when you logged in to the back end

        • Justin Silver says:

          The default configuration for W3TC is to not cache anything when logged in, so that behavior would make sense unless you changed something. You might also want to confirm that it is not your browser caching the page and is actually W3TC responding with cached content. I find using `curl` and the command line is useful for debugging. since you can control exactly what is sent to the server.

  10. Riccardo says:

    Thanks Justin,
    I tried your code but want I get is the php code on my page. echo ‘The time is ‘.date( ‘H:i:s’, time() );
    I tried to put the “define..” in both my wp-config and them on my theme function.php, without any luck.
    Em I doing something wrong?

  1. July 7, 2015

    […] about using fragmented caching with W3 Total Cache you can check out the following links: W3 Total Cache Fragment Caching in WordPress Implementing Page Fragment Caching in W3 Total Cache WordPress.org support […]

Leave a Reply

Your email address will not be published. Required fields are marked *