Skip to content
Justin SilverTECHNOLOGY, TRAVEL & EVERYTHING IN BETWEEN

WordPress

W3 Total Cache Fragment Caching in WordPress

October 31, 2014

W3 Total Cache, also known as W3TC, is a very powerful caching plugin for WordPress which is notoriously slow without tuning. For guest users much of your content will typically be static which makes serving up cached content to these users a pretty good idea. But what if you want part of the page to be be dynamic while most of the page is cached? Fragment caching to the rescue.

Configure W3 Total Cache

First you will need to make sure W3TC is properly configured for fragment caching. To enable it, from your WordPress Admin, visit Performance > General Settings and look under the Page Cache section.  Make sure that the Page cache method is set to Disk: Basic and save your settings. Next visit Performance > Page Cache and scroll down to the Advanced section. Here you will need to check the box next to Late initialization to enable it. Notice the help text under it:
Enables support for WordPress functionality in fragment caching for the page caching engine. Use of this feature may increase response times.

Set W3TC_DYNAMIC_SECURITY Constant

For security reasons W3TC requires that you create a constant named W3TC_DYNAMIC_SECURITY and pass it to the sections of code you would like it to ignore (and execute!). An obvious choice for this is wp-config.php, but you might also have success with /wp-content/mu-plugins, a custom plugin, your theme's functions.php, or even inline before your fragment - though the latter isn't really recommended. You can set this to a hardcoded value, or use something random for each request.

define( 'W3TC_DYNAMIC_SECURITY', 'SOME_SECURE_STRING_YOU_CREATE' );

Update your template

The final step is to update your template files to indicate where you want W3TC to *not* cache the page. This is done by using an HTML comment called `mfunc`. You pass it your W3TC_DYNAMIC_SECURITY and include any PHP you want to execute (without <?php> tags!).

&lt;!--mfunc &lt;?php echo W3TC_DYNAMIC_SECURITY; ?&gt; --&gt;
echo 'The time is '.date( 'H:i:s', time() );
&lt;!--/mfunc &lt;?php echo W3TC_DYNAMIC_SECURITY; ?&gt; --&gt;

Conversation 45

RiccardoMay 6, 2015
Thanks Justin, I tried your code but want I get is the php code on my page. echo 'The time is '.date( 'H:i:s', time() ); I tried to put the "define.." in both my wp-config and them on my theme function.php, without any luck. Em I doing something wrong?
Justin SilverMay 6, 2015
Sometimes all it takes is posting a comment on the Internet for code to start working :)
Justin SilverJune 2, 2015
Sorry to hear that... it worked for me in my testing. Is it just caching the entire page? Not caching at all? Any errors?
markJune 22, 2015
Hi, Will it work with Clouflare? Is there also a way to cache different pages for each country? Thanks
Justin SilverJune 22, 2015
This code is specifically designed to work with the W3 Total Cache plugin (W3TC) so I'm not sure how it would work in conjunction with CloudFlare. If you are using CloudFlare exclusively then this wouldn't have any effect, and unfortunately I'm not familiar enough to make any suggestions. If you used something like WordPress Multi Language (WPML) then you should be able to cache different language pages using W3TC. Should also work with custom solutions.
markJune 22, 2015
I actually have both, Cloudflare and W3TC. I prefer not to install more plugins. Maybe I can define cloudflare not to cache and then follow your solution. Think this way it will work?
Justin SilverJune 23, 2015
Like I mentioned I'm not that familiar with CloudFlare, but that certainly sounds reasonable. If you can tell CloudFlare to not cache a page then it will fall through to your server. Assuming W3TC is properly configured it should serve pages from your cache, and in that case the fragment caching would be supported as I included in my blog post.
JackTheKnifeJune 29, 2015
Same issue for me. It shows once then is cached as a plain text not excluded from caching. Works fine when you logged in to the back end
Justin SilverJune 29, 2015
The default configuration for W3TC is to not cache anything when logged in, so that behavior would make sense unless you changed something. You might also want to confirm that it is not your browser caching the page and is actually W3TC responding with cached content. I find using `curl` and the command line is useful for debugging. since you can control exactly what is sent to the server.
JackTheKnifeJune 29, 2015
I have checked test template via CURL and it is same - code output as a plain text not executed PHP
JackTheKnifeJune 29, 2015
Also I have checked /cache/page/ folder and cached page is with plain code as well.
WordPress Cache Plugins Are Nice But&#8230;July 7, 2015
[…] about using fragmented caching with W3 Total Cache you can check out the following links: W3 Total Cache Fragment Caching in WordPress Implementing Page Fragment Caching in W3 Total Cache WordPress.org support […]
RameshFebruary 9, 2016
I just want to disable cache for specific php files, how can I do?
Justin SilverFebruary 9, 2016
Hi Ramesh, The article above is for excluding certain parts of a page from the cache, but if you would like to exclude an entire page you can do it from the Page Cache settings (in the admin) by adding its path (or partial path using * as a wildcard. If you want to exclude a page template from ever being cached, you need to add the following constant declaration to the template: [php] define( 'DONOTCACHEPAGE', true ); [/php] Good luck!
RameshFebruary 10, 2016
Hi Justin, I added define( 'DONOTCACHEPAGE', true ); in wp-config, and set W3TC, it working I think.
Justin SilverFebruary 10, 2016
If you set that constant in wp-config.php it will disable caching on *all* pages, not just a specific template. If you want to just disable it for pages, for example, you would put it in page.php in your theme.
RameshMarch 16, 2016
Hi Justin, When I adding define( ‘DONOTCACHEPAGE’, true ); in Footer.PHP then it do not cache all the page, any I idea how to solve it... Plugin - WP Super Cache Thank you for replying fast.
LinaMarch 26, 2016
Unfortunately its not working for me either, it always shows the error: "unable to execute code:" and then repeats the code within mfunc function. Before giving you details, I just wanted to know if fragment caching is only available for Pro Version of W3TC? Because I have only the free version...Many thanks in advance and happy easter :)
Justin SilverMarch 27, 2016
Hi Lina, I was using this with the free version of W3TC, but unfortunately I'm not sure why it wouldn't be working for you. Good luck!
AdrianMay 12, 2016
I read elsewhere that you need to add mfunc to the "Ignored comment stems:" in the HTML & XML section of "Minify" settings.
TuanJuly 25, 2017
Thank for post, but the last thing, where i can post that "mfunc" in my wordpress website ?
TuanJuly 26, 2017
I was try like your demo, but i have problem it's not show anything. May be problem because i use FastCGI ?
Justin SilverJuly 26, 2017
It goes in the WordPress PHP page template.
Tuan TranAugust 6, 2017
Thanks for share, i was try but it don't work with me. It just plain text and i don't know what to do. I was add 'mfunc' and define( 'W3TC_DYNAMIC_SECURITY', md5( rand( 0, 999999 ) ) );
GabrielJanuary 3, 2018
Nice one, i tried implementing on my new WordPress blog @ https://myschoolinfo.com.ng it works
Frederick TownesFebruary 5, 2018
There is a best practice documented in the FAQ of W3TC for related use cases. Our recommended use case doesn't regenerate itself. W3TC_DYNAMIC_SECURITY should have a static value. Please refer to the FAQ for usage details.
Justin SilverFebruary 5, 2018
I assumed it needed to be a dynamic value because of the constant name. Can you send a link to the FAQ and I can update the post accordingly? I did a Google search but wasn't able to find it - the FAQ on the Wordpress repo doesn't mention fragment caching. Thanks!
پروژه متلبFebruary 10, 2018
I think the WordPress w3 version will create a massive revolution in website design
EnzoMarch 31, 2018
its possible apply these tag to exclude a function operation in functions.php where export to shortcode?
انجام پروژه متلبJune 3, 2018
thanks jastin It's possible to give me more guidance, I did not get very precise ؟
Gido MandersAugust 30, 2018
Here it is: https://github.com/Auctollo/w3-total-cache/wiki/FAQ:-Developers#how-do-i-implement-page-fragment-caching
farshidMay 10, 2019
thank you Justin Silver i want to optimize my website and reduce pageload time but it not impusible eventhow my page have less weight my page load time not reduce frome 7sec. please check my web and help me🌹🌹🌹 https://pinpaper.ir/
FrankAugust 15, 2019
i tried it for https://www.thenaijainfo.com/ but it didnt work out well making my site still loading slowly
Justin SilverSeptember 13, 2019
Hi Frank, just fragment caching won't speed up your site but using caching should, generally speaking. It's possible that you are using a plugin that doesn't work with a cache or is poorly designed otherwise so running a SQL profiler and PHP profiler on your site to see what exactly is slow might help. Good luck!
arti lirikDecember 18, 2019
Finally I found a suitable article, thank you for the tutorial, I have already applied it to my wordpress
mehdiJune 8, 2020
hi i enable leazy load but its dont work in my site what can i do? my fragment catch was enable and its work good now fragment catch disable and leazy load dont work more
CrimeanFebruary 2, 2024
I do not use the minify feature on w3 total cache. But wp-polls plugin is not working correctly. How do you think I should do it?