When implementing a Page Fragment Caching Exception, you are telling W3 Total Cache to bypass cache for a particular section of content. A great use case for this is dynamic content that you wish to update real time, separate of the page caching itself. The following article will walk you through the process.
- Select your fragment caching method in Performance → General Settings
- We recommend you choose a memory-based caching backend such as Redis or memcached
- Enable “late initialization” on the Page Cache settings page
- Add define(‘W3TC_DYNAMIC_SECURITY’, ‘mycode’); to your wp-config.php file
And finally, use this format to implement the caching exception in your template for the dynamic content:
Implementing a Page Fragment Caching Exception for Comments in Twenty Twenty
The following example demonstrates how to configure an exception group for blog post comments using the default WordPress theme, Twenty Twenty and W3 Total Cache.
- Twenty Twenty theme
- Twenty Twenty child theme
Install both themes in your WordPress installation and activate the Twenty Twenty child theme.
Copy the comments.php file from the Twenty Twenty theme over to the child theme root directory.
Install and activate W3 Total Cache and set your Pro license.
Navigate to Performance > General Settings and enable Page Cache. If you do not have memory based caching available on your server (Redis or Memcached) the select Disk: Basic for the Page Cache Method.
Access your Page Cache settings by navigating to Performance > Page Cache in your WordPress dashboard and enable Late initialization under the Advanced section.
Add the code define(‘W3TC_DYNAMIC_SECURITY’, ‘comments’); to the wp-config. php file associated with your website just before the line /* That’s all, stop editing! Happy publishing. */
Open comments.php located in your child theme directory using your preferred text editor.
Locate the wp_list_comments function and wrap it in your <!– mfunc comments–> opening and closing tags. Your code should look like this:
W3 Total Cache
You haven't seen fast until you've tried PRO
Full Site CDN + Additional Caching Options
Advanced Caching Statistics, Purge Logs and More
Everything you need to scale your WordPress Website and improve your PageSpeed.