Implementing Page Fragment Caching in W3 Total Cache

Page fragment caching is a must-have function for cache plugins like W3 Total Cache, it’s a shame that such functions didn’t get much attention.

W3 Total Cache answered the “How do I implement page fragment caching?” question in the Developers section in their plugin FAQ page:

First you need to define W3TC_DYNAMIC_SECURITY in your wp-config.php file.

define('W3TC_DYNAMIC_SECURITY', 'somesecurestring');

Edit your templates with the following syntax to ensure that dynamic features remain so. Replace W3TC_DYNAMIC_SECURITY with content of the constant or use echo to print constant:

Example 1:
<!-- mfunc W3TC_DYNAMIC_SECURITY any PHP code --><!-- /mfunc W3TC_DYNAMIC_SECURITY -->
Example 2:
<!-- mfunc W3TC_DYNAMIC_SECURITY -->any PHP code<!-- /mfunc W3TC_DYNAMIC_SECURITY -->
Example 3:
echo rand();
<!--/mfunc W3TC_DYNAMIC_SECURITY -->
Example 4:
<!-- mclude W3TC_DYNAMIC_SECURITY path/to/file.php --><!-- /mclude W3TC_DYNAMIC_SECURITY -->
Example 5:
<!-- mclude W3TC_DYNAMIC_SECURITY -->path/to/file.php<!-- /mclude W3TC_DYNAMIC_SECURITY -->
Be aware that WordPress functions will not be available.

The answer itself is pretty much clear except it forgot to mention a most important step: YOU NEED TO ADD ‘mfunc’ TO THE ‘Ignored comment stems’ SETTINGS.

Source: WooThemes

Without this step you’ll waste 100 hours on testing the page fragment caching and never getting it worked. Thanks to WooThemes pointed it out and hope I can save someone else’s 100 hours.

Added on March 27:

After couple of days I found the fragment caching still acted weird. Sometimes it worked fine but sometimes didn’t. After some research I believe it’s related to the “Page cache method” I set.

The site I need to implement fragment caching is sometimes very high volume. So if I set the “Page cache method” to “Disk: Enhanced”, which is recommended by W3TC, the old cache files would be used while the new cache files are being generating. Read explanation here. Changed the setting to “Disk: Basic” can fix the issue.

The other thing to note is if you need to use WordPress functions in your fragment caching block, you should enable the “Late initialization”, which may increase response time.

Now the fragment caching finally worked on my client’s website. I highly recommend anyone who needs a powerful WordPress cache plugin, just go with W3 Total Cache. WP-Rocket is great but it doesn’t support fragment cache yet.

Share the post:Tweet about this on TwitterShare on Google+1Share on Facebook2Share on LinkedIn0
I'm a senior web developer helping clients build their websites to grow businesses. Currently I'm based in Taipei, Taiwan. I write things about WordPress, AngularJS and life. Whenever you'd like to find someone to talk about these topics, just get in touch!


Submit a comment
  • Thank you for sharing information about Page Fragment Caching. Do you know if there is a way to wrap Page Fragment Caching in a page template with a condition to enable it only in production environment because when we develop a website we don’t want to be forced to enable page caching. I search information about this but I didn’t find anything.

    • Hi, François, I know we can check if the constant W3TC is defined to see if W3TC is activated. If you need to specifically check just the “page cache”, checking if a certain option exists (with get_option()) is the best way I can think.

    • OH, ha, my bad! Yes of course we should check if the constant W3TC_DYNAMIC_SECURITY defined. Thanks for the info.

  • This is awesome, thank you!

    Can someone clarify if the debug timestamp changes on the pages using the ‘mfunc’ changes on refresh as if the page isn’t cached?

    • Hi Yazmin, do you mean the timestamp added by W3 Total Cache? It shouldn’t be updated every time the page refreshed. If you’re logged out and the timestamp still gets updated on refreshes, I think the page cache is not working.

      • Thank you, yes, that is what I meant. I had to wait for the page to cache.

        So, I think I’m almost there with this, but not quite. I have the constant set in wp-config, and I have my code in the template, within the mfunc comment tags (without the opening/closing PHP tags), I’ve set page cache to “basic” and I’ve selected late initialization.

        I know it’s hard to debug without viewing the code, but anything else you could think of off the top of your head that may not be allowing the page to fragment cache that section?

        • Yazmin, it looks like the page cache is not working at all no matter how the fragment cache is set. The fragment cache is for you to “exclude” some parts of the page cache.

          Some shared hostings don’t allow cache plugins like W3TC since it really consumes a lot of server resources. That’s the only thing I can think of why the page cache won’t work in your case.

          • Thank you. I’ll keep that in mind for other sites.

            On this site, however, I did more fiddling and realized our repo was tracking settings for the W3TC plugin and kept changing us over to Disk:Enhanced and generating those cache files. Once I deleted the old cache files for the two pages in question, the fragment caching worked as expected throughout the day.

            Again, many thanks! Wish W3TC had this feature better documented in the first place. Would have saved me 2 weeks of fiddling with the plugin. Your post was a lifesaver.

          • Hi Yazmin, glad you figured that out and thanks for the sharing!

  • I have followed this guide but it doesnt seem to be working. I am using latest version of W3TC and WordPress. Are you sure the fragment caching is not for W3TC Pro because on their website, its only mentioned on the W3TC Pro pages.

  • @AFAQ – did you find out if fragment caching is avaiable only for Pro? I don’t have Pro but would like to use it but cant get it to work…

  • Hello, thank for your post.
    I need help how to find where to use the W3TC_DYNAMIC_SECURITY in my website ?
    How to find in theme, plugin …

Submit a comment

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