Tagged: Fragment Cache
- AuthorPosts
- November 28, 2025 at 2:12 pm #166198
Cinzia
GuestHi,
this code integrates W3 Total Cache’s “fragment cache” with a small helper, and then uses it to cache the current time for 60 seconds.// Fragment cache helper function
function fragment_cache($key, $function) {
$group = ‘capelli24’;
$key = apply_filters(‘fragment_cache_prefix’, ‘fragment_cache_’) . $key;
if (function_exists(‘w3tc_fragmentcache_start’)) {
if (!w3tc_fragmentcache_start($key, $group)) {
call_user_func($function);
w3tc_fragmentcache_end($key, $group);
}
} else {
call_user_func($function);
}
}// Register fragment cache group with 60s TTL
add_action(‘w3tc_register_fragment_groups’, function() {
if (function_exists(‘w3tc_register_fragment_group’)) {
w3tc_register_fragment_group(‘capelli24’, [], 60);
}
});// Using the helper in a template to cache the current time
<?php
// Shows the current time, cached for 60 seconds using W3TC fragment cache
fragment_cache(‘band’, function() { ?>
<?php echo date(‘H:i:s’); ?>
<?php }); ?>In the WordPress admin area, where page cache is disabled, this code works as expected: the time is updated every 60 seconds.
On the public front end, where W3TC page cache is enabled, the time does not refresh every 60 seconds.
I always see the same HTML that was generated when the page cache entry was created, and the PHP fragment is not re-executed while the page cache is warm.
It seems that the full page cache is served before the fragment cache logic runs, so the fragment TTL is never applied on cached pages.I am currently testing this in a local development environment with the constant define(‘W3TC_PRO_DEV_MODE’, true); set.
Could you please confirm if fragment caching is supposed to work together with page caching in this scenario, or if pages that use fragment_cache() must be excluded from page cache?
If there is a recommended configuration to allow this fragment to respect its 60-second TTL on the front end, I would like to apply it.Thank you.
Cinzia
November 28, 2025 at 2:16 pm #166423Marko Vasiljevic
KeymasterHello Cinzia
Thank you for reaching out, and I am happy to help!
Just to confirm, are you using a Fragment Cache extension in the W3 Total Cache to set this up, or are you trying to set up Page Fragment Caching?
Thank you for the clarification!
December 15, 2025 at 9:16 am #166625Cinzia
GuestHi Marko,
I am using the Fragment Cache functionality provided by W3 Total Cache (the built-in fragment caching extension).
The code calls w3tc_fragmentcache_start() and w3tc_fragmentcache_end() directly, and the group is registered through w3tc_register_fragment_group() with a 60-second TTL.My goal is to verify whether fragment caching is expected to work when the page cache is enabled on the front end, or whether any page that contains fragment_cache() must be excluded from page caching.
At the moment, with page cache enabled, the fragment does not refresh according to its TTL.Could you confirm the expected behavior and advise on the recommended configuration to make the fragment respect its TTL?
Thank you.
Cinzia
- AuthorPosts
- The topic ‘W3 Total Cache fragment cache TTL not respected when page cache is enabled’ is closed to new replies.