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:
<!--MFUNC W3TC_DYNAMIC_SECURITY -->
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.
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.
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.Thank you Yoren. I finally found what I’m looking for in the following article http://www.contrid.co.za/2015/05/w3-total-cache-fragment-caching/.
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.
Yes I’m pretty sure it still works well at the latest version of W3TC. Feel free to submit a ticket on plugin forum to get supports from W3TC team.
I’m also wondering if this only works for Pro version ?
Don’t think so, I have it worked with the free version of W3TC.
@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 …
I was try but mfunc show plant text, what i need to do ?
Realy? It works with the free version of W3TC?
its possible apply these tag to exclude a function operation in functions.php where export to shortcode?
I think shortcode is very tricky in such scenario. I’ve done something like remove the shortcode but registered a similar one to prevent the content being cached. Maybe something like that.