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.


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.


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

44 Responses

  1. Alex says:

    Thank you! Helped me a lot .

  2. mehdi says:

    hi i enable leazy load but its dont work in my site what can i do?

    my fragment catch was enable and its work good now fragment catch disable and leazy load dont work more

  3. my site says:

    thanks a lot sire

  4. arti lirik says:

    Finally I found a suitable article, thank you for the tutorial, I have already applied it to my wordpress

  5. Frank says:

    i tried it for https://www.thenaijainfo.com/ but it didnt work out well making my site still loading slowly

    • User Avatar Justin Silver says:

      Hi Frank, just fragment caching won’t speed up your site but using caching should, generally speaking. It’s possible that you are using a plugin that doesn’t work with a cache or is poorly designed otherwise so running a SQL profiler and PHP profiler on your site to see what exactly is slow might help. Good luck!

  6. farshid says:

    thank you Justin Silver
    i want to optimize my website and reduce pageload time but it not impusible eventhow my page have less weight my page load time not reduce frome 7sec. please check my web and help me🌹🌹🌹

  7. thanks jastin
    It’s possible to give me more guidance, I did not get very precise ؟

  8. Enzo says:

    its possible apply these tag to exclude a function operation in functions.php where export to shortcode?

  9. I think the WordPress w3 version will create a massive revolution in website design

  10. There is a best practice documented in the FAQ of W3TC for related use cases. Our recommended use case doesn’t regenerate itself. W3TC_DYNAMIC_SECURITY should have a static value. Please refer to the FAQ for usage details.

  11. Gabriel says:

    Nice one, i tried implementing on my new WordPress blog @ https://myschoolinfo.com.ng it works

  12. 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 ) ) );

  13. Tuan says:

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

  14. Adrian says:

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

  15. 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 🙂

    • User Avatar 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!

  16. Ramesh says:

    Hi Justin,

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

  17. Ramesh says:

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

    • User Avatar 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!

  18. John says:

    very useful article

  19. mark says:


    Will it work with Clouflare?

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


    • User Avatar 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?

        • User Avatar 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.

  20. nev says:

    This does NOT work for me.

    • User Avatar 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

        • User Avatar 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.

  21. 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 *