{"id":173355,"date":"2026-06-30T16:14:22","date_gmt":"2026-06-30T20:14:22","guid":{"rendered":"https:\/\/www.boldgrid.com\/support\/?p=173355"},"modified":"2026-06-30T16:15:10","modified_gmt":"2026-06-30T20:15:10","slug":"how-to-implement-a-page-fragment-caching-exception-in-w3-total-cache-2-10-0","status":"publish","type":"post","link":"https:\/\/www.boldgrid.com\/support\/w3-total-cache\/how-to-implement-a-page-fragment-caching-exception-in-w3-total-cache-2-10-0\/","title":{"rendered":"How to Implement a Page Fragment Caching Exception in W3 Total Cache 2.10.0+"},"content":{"rendered":"<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/REC-html40\/loose.dtd\">\n<?xml encoding=\"utf-8\" ?><html><body><p>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 in real time, separate from the page caching itself. The following article will walk you through the process.<\/p>\n<p><strong>As of W3 Total Cache 2.10.0<\/strong>, Page Fragment Cache tags work differently. Instead of placing PHP or a file path <em>inside<\/em> the tag, you register a callback once and reference it from the tag by name. W3 Total Cache runs only your registered callback when it serves the cached page. Tags from older versions that still contain raw PHP or a file path are no longer run, so they must be migrated using the steps below. <em>(Using a version before 2.10.0? See <a href=\"https:\/\/www.boldgrid.com\/support\/w3-total-cache\/how-to-implement-page-fragment-caching-exception-in-w3-total-cache\/\">How to Implement a Page Fragment Caching Exception (versions before 2.10.0)<\/a>.)<\/em><\/p>\n<div class=\"boldgrid-section\" style=\"margin-top: 60px;\">\n<div class=\"container w3tc-radius\" style=\"border-width: 0px; background-image: url('https:\/\/www.boldgrid.com\/wp-content\/uploads\/2023\/07\/upsell-darker-alt.png'); background-size: cover; background-position: 50% 5%;\" data-image-url=\"https:\/\/www.boldgrid.com\/wp-content\/uploads\/2023\/07\/upsell-darker-alt.png\">\n<div class=\"row upsell-column\" style=\"padding-top: 2em; padding-bottom: 2em; color: #ffffff;\">\n<div class=\"col-lg-1 col-md-1 col-sm-12 col-xs-12 hidden-sm\"> <\/div>\n<div class=\"col-lg-5 col-md-6 col-xs-12 col-sm-7\">\n<p class=\"h3\" style=\"color: #ffffff; line-height: 40px; margin-top: 0;\"><strong>Speed Up Your WordPress Website.<\/strong><\/p>\n<p style=\"color: #ffffff;\" class=\"h4\">A Difference of Just 100ms in Page Load Speeds Can Cause a Visitor to Prefer Your Competitor&rsquo;s Website.<\/p>\n<\/div>\n<div class=\"col-lg-6 col-md-5 col-sm-5 col-xs-12 text-center align-column-center\">\n<p class=\"\" style=\"border-width: 0px;\"><a class=\"button-w3tc-white\" style=\"border: 2px solid #30BEC4; font-size: 18px; padding: 15px 30px;\" href=\"https:\/\/www.boldgrid.com\/w3-total-cache\/#pricing\">View Plans<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul>\n<li>Select your page cache method in Performance &rarr; General Settings\n<ul>\n<li>We recommend a <a href=\"https:\/\/www.boldgrid.com\/support\/w3-total-cache\/configuring-w3-total-cache-for-wordpress-with-vps-or-dedicated-hosting\/\">memory-based caching backend such as Redis or memcached<\/a>. <strong>Disk: Basic<\/strong> also works. <strong>Disk: Enhanced is not supported<\/strong> for fragment exceptions, because it serves cached pages without running PHP.<\/li>\n<\/ul>\n<\/li>\n<li>Enable &ldquo;late initialization&rdquo; on the Page Cache settings page<\/li>\n<li>Add <code>define('W3TC_DYNAMIC_SECURITY', 'mycode');<\/code> to your wp-config.php file (change <code>mycode<\/code> to your own unique value)<\/li>\n<li>Register a callback for each fragment using the <code>w3tc_dynamic_callbacks<\/code> filter<\/li>\n<\/ul>\n<p>And finally, use this format to implement the caching exception. First, register the callback in your theme&rsquo;s functions.php (or a small site plugin):<\/p>\n<pre>\/\/ functions.php\r\nadd_filter(\r\n      'w3tc_dynamic_callbacks',\r\n      function ( $callbacks ) {\r\n              $callbacks['my_fragment'] = function ( $args, $kind ) {\r\n                      \/\/ Return the dynamic HTML for this fragment.\r\n                      \/\/ $args = JSON from the tag (decoded to an array); $kind = 'mfunc' or 'mclude'.\r\n                      return somefunction();\r\n              };\r\n              return $callbacks;\r\n      }\r\n);<\/pre>\n<p>Then emit the tag in your template where the dynamic content should appear, referencing the callback by name with <code>call:&lt;slug&gt;<\/code>. W3 Total Cache adds the integrity signature automatically, so you never write it yourself:<\/p>\n<pre>&lt;!-- mfunc mycode call:my_fragment {} --&gt;&lt;!-- \/mfunc mycode --&gt;<\/pre>\n<p>Your callback must <strong>return<\/strong> a string of HTML (don&rsquo;t <code>echo<\/code>). To pass data into a fragment, include JSON in the tag &mdash; for example <code>call:my_fragment {\"id\":42}<\/code> &mdash; and read <code>$args['id']<\/code> in the callback. <code>mclude<\/code> works the same way: register a slug that returns the included output (for example with <code>ob_start()<\/code> \/ <code>ob_get_clean()<\/code>) and reference it with <code>call:&lt;slug&gt;<\/code>; raw file paths are no longer included directly. If you use Minify, add <code>mfunc<\/code> and <code>mclude<\/code> to the <em>Ignored comment stems<\/em> setting. Third-party plugins or themes that shipped their own legacy tags must adopt this same filter &mdash; a site owner cannot re-enable the old in-tag-code behavior.<\/p>\n<hr>\n<h2 id=\"implementing-a-page-fragment-caching-exception-for-comments-in-twenty-twenty\">Implementing a Page Fragment Caching Exception for Comments in Twenty Twenty<\/h2>\n<p>The following example demonstrates how to render blog post comments dynamically on an otherwise cached page using the default WordPress theme, Twenty Twenty, and W3 Total Cache 2.10.0 or later.<\/p>\n<p>Resources:<\/p>\n<ul>\n<li>Twenty Twenty theme<\/li>\n<li>Twenty Twenty child theme<\/li>\n<\/ul>\n<p>Install both themes in your WordPress installation and activate the Twenty Twenty child theme.<\/p>\n<p>Copy the comments.php file from the Twenty Twenty theme over to the child theme root directory.<\/p>\n<p>Install and activate W3 Total Cache and set your Pro license.<\/p>\n<p>Navigate to <b>Performance<\/b> &gt; <b>General Settings<\/b> and enable <b>Page Cache<\/b>. If you do not have memory based caching available on your server (Redis or Memcached) then select <b>Disk: Basic<\/b> for the <b>Page Cache Method<\/b>. (Disk: Enhanced is not supported for fragment exceptions.)<\/p>\n<p>Access your Page Cache settings by navigating to <b>Performance<\/b> &gt; <b>Page Cache<\/b> in your WordPress dashboard and enable <b>Late initialization<\/b> under the <b>Advanced<\/b> section.<\/p>\n<p>Add the code <b>define(&lsquo;W3TC_DYNAMIC_SECURITY&rsquo;, &lsquo;comments&rsquo;);<\/b> to the wp-config.php file associated with your website just before the line <b>\/* That&rsquo;s all, stop editing! Happy publishing. *\/<\/b><\/p>\n<p>Register the comments callback by adding the following to your child theme&rsquo;s functions.php file:<\/p>\n<pre>\/\/ Twenty Twenty child theme &mdash; functions.php\r\nadd_filter(\r\n      'w3tc_dynamic_callbacks',\r\n      function ( $callbacks ) {\r\n              $callbacks['comments_list'] = function ( $args, $kind ) {\r\n                      ob_start();\r\n                      wp_list_comments(\r\n                              array(\r\n                                      'walker'      =&gt; new TwentyTwenty_Walker_Comment(),\r\n                                      'avatar_size' =&gt; 120,\r\n                                      'style'       =&gt; 'div',\r\n                              )\r\n                      );\r\n                      return ob_get_clean();\r\n              };\r\n              return $callbacks;\r\n      }\r\n);<\/pre>\n<p>Open comments.php located in your child theme directory using your preferred text editor. Locate the <code>wp_list_comments<\/code> call and replace it with the matching tag, which references the callback you just registered. Your code should look like this:<\/p>\n<pre>&lt;!-- mfunc comments call:comments_list {} --&gt;&lt;!-- \/mfunc comments --&gt;<\/pre>\n<p>&nbsp;<\/p>\n<\/body><\/html>\n","protected":false},"excerpt":{"rendered":"<p>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 in real time, separate from the page caching itself. The following article will walk you through the process. As [&hellip;]<\/p>\n","protected":false},"author":1307,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"bgseo_title":"","bgseo_description":"","bgseo_robots_index":"index","bgseo_robots_follow":"follow","footnotes":""},"categories":[749,740,692],"tags":[],"class_list":["post-173355","post","type-post","status-publish","format-standard","hentry","category-fragment-cache","category-page-cache","category-w3-total-cache"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.boldgrid.com\/support\/wp-json\/wp\/v2\/posts\/173355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.boldgrid.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.boldgrid.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.boldgrid.com\/support\/wp-json\/wp\/v2\/users\/1307"}],"replies":[{"embeddable":true,"href":"https:\/\/www.boldgrid.com\/support\/wp-json\/wp\/v2\/comments?post=173355"}],"version-history":[{"count":5,"href":"https:\/\/www.boldgrid.com\/support\/wp-json\/wp\/v2\/posts\/173355\/revisions"}],"predecessor-version":[{"id":173360,"href":"https:\/\/www.boldgrid.com\/support\/wp-json\/wp\/v2\/posts\/173355\/revisions\/173360"}],"wp:attachment":[{"href":"https:\/\/www.boldgrid.com\/support\/wp-json\/wp\/v2\/media?parent=173355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.boldgrid.com\/support\/wp-json\/wp\/v2\/categories?post=173355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.boldgrid.com\/support\/wp-json\/wp\/v2\/tags?post=173355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}