W3 Total Cache Fragment Caching in WordPress
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.
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
<!--mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> --> echo 'The time is '.date( 'H:i:s', time() ); <!--/mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> -->
Thank you! Helped me a lot .
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
thanks a lot sire
Finally I found a suitable article, thank you for the tutorial, I have already applied it to my wordpress
i tried it for https://www.thenaijainfo.com/ but it didnt work out well making my site still loading slowly
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!
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🌹🌹🌹
It’s possible to give me more guidance, I did not get very precise ؟
its possible apply these tag to exclude a function operation in functions.php where export to shortcode?
I think the WordPress w3 version will create a massive revolution in website design
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.
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!
Here it is: https://github.com/Auctollo/w3-total-cache/wiki/FAQ:-Developers#how-do-i-implement-page-fragment-caching
Nice one, i tried implementing on my new WordPress blog @ https://myschoolinfo.com.ng it works
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 ) ) );
Thank for post, but the last thing, where i can post that “mfunc” in my wordpress website ?
I was try like your demo, but i have problem it’s not show anything. May be problem because i use FastCGI ?
It goes in the WordPress PHP page template.
I read elsewhere that you need to add mfunc to the “Ignored comment stems:” in the HTML & XML section of “Minify” settings.
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 🙂
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!
I added define( ‘DONOTCACHEPAGE’, true ); in wp-config, and set W3TC, it working I think.
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.
thank you, Justin, I shall try
Super kp. Kp is always perfect
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.
I just want to disable cache for specific php files, how can I do?
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:
very useful article
Will it work with Clouflare?
Is there also a way to cache different pages for each country?
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.
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?
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.
This does NOT work for me.
Sorry to hear that… it worked for me in my testing. Is it just caching the entire page? Not caching at all? Any errors?
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
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.
I have checked test template via CURL and it is same – code output as a plain text not executed PHP
Also I have checked /cache/page/ folder and cached page is with plain code as well.
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?
Sorry Justin, now it works
Sometimes all it takes is posting a comment on the Internet for code to start working 🙂