Tagged: Page Fragment Caching
- AuthorPosts
- September 17, 2024 at 11:23 am #142319Mike ButcherGuest
Are both ‘Page Fragment Caching’ and ‘Fragment Caching’ pro features?
I’m seeing that ‘Page Fragment Caching’ might be a free option but having trouble enabling it.September 17, 2024 at 11:29 am #142385Marko VasiljevicKeymasterHello Mike,
Thank you for reaching out and I am happy to help!
In W3 Total Cache, Fragment Caching is a Pro feature, the Page Fragment Cache is a different feature and it’s free.
“Page Fragment Cache” dynamically loads the content (in real-time) on your cached pages by parsing them for specific tags you manually embed. Despite the naming, this feature should not be misunderstood with “Fragment Cache”, the premium feature. “Page Fragment Cache” is related to the parsing of templates. “Fragment Cache” adds more complex behaviors to a template/theme or plugin. It extends the transient API in WordPress to provide caching and purge policy management for fragments you manually create. It also enables fragment groups, defined by a plugin or theme, which include common operations to be cached using transients.Page Fragment Cache cannot be used in combination with Page Cache mode Disk: Enhanced because that mode will load the cached page directly from Apache or Nginx. We recommend using Disk: Basic, which will run PHP to process the requests with the benefit of speeding up responses by loading a cached page instead of building the page from scratch. You can use Page Cache engines Redis or Memcached on dedicated or VPS servers as well.
Note: The comment/code blocks should be added to your theme template files as HTML and not within the PHP open () tags, as it will cause a PHP parse error/syntax error. Do not include any sensitive information in your code fragments; the code can be revealed if W3 Total Cache is deactivated, the Page Cache is changed to use an incompatible storage engine, or the defined W3TC_DYNAMIC_SECURITY string value is changed or not defined.
How does page fragment cache work?
W3TC will search for comments explained below and run the PHP code dynamically before sending it to the browser.The Page Fragment Cache feature is perfect to display things like the current date and time or content based on the visitor’s session. To be able to use Page Fragment Cache, you define a static variable in your wp-config.php file such as define( ‘W3TC_DYNAMIC_SECURITY’, ‘mysecurestring’ );. Please change the string value “mysecurestring” to something unique. Also add mfunc and mclude to Ignored comment stems: in the Minify settings if it is enabled.
You are then able to use the mfunc and mclude comments as a replacement of the PHP tags like this:
<!-- mfunc mysecurestring --> if ( is_user_logged_in() ) { echo '<a href="logout">Logout</a>'; } else { echo '<a href="login">Login</a>'; } <!-- /mfunc mysecurestring -->
<!-- mclude mysecurestring -->path/to/file.php<!-- /mclude mysecurestring -->
You could also shorten it a little bit if you prefer one line of PHP code:<!-- mfunc mysecurestring echo date( 'Y-m-d H:i' ); --><!-- /mfunc mysecurestring -->
<!-- mclude mysecurestring path/to/file.php --><!-- /mclude mysecurestring -->
If you want to use plugin functions or WordPress functions, like is_user_logged_in() in the example above, you have to enable Late initialization: in the Page Cache settings. Other functions, like the ones you defined in your theme, are not available. They should be defined within the fragment to be able to use them.I hope this helps!
- AuthorPosts
- The topic ‘Page Fragment Caching’ is closed to new replies.