W3 Total Cache is a great plugin for speeding up your WordPress website with page cache, database cache, minifying JS and CSS, object cache, content delivery network and more.
A problem could arise with dynamic code and a certain part of code might be cached with W3 Total Cache which you don’t want to cache. You can use the MFUNC fragment caching in your code to prevent this.
Using W3 Total Cache MFUNC Fragment Caching
W3 Total Cache lets you use it’s MFUNC fragment caching to specify cached and not-cached content but it also requires a
W3TC_DYNAMIC_SECURITY constant for security reasons.
In addition to that, two things are required in order to use fragment caching:
- Set the page cache to Disk: Basic
- Turn on late initialisation under the Page Cache settings section.
Here is an example of using W3TC MFUNC comments:
CODE 1 above is the code to execute with W3TC enabled and the page currently cached.
CODE 2 above is the code to execute with W3TC disabled or the page not cached.
Of course, in the above example CODE1 and CODE2 will most likely be the same code.
For example, say you have PHP code that echoes “This is my website”. You can implement it like this using W3 Total Cache mfunc fragment caching:
Dynamic HTML content fragment caching
Good luck with it!