Last updated on

Social layer, personalization, and e-commerce are common elements of highly dynamic websites. That means that there are other solutions than caching entire pages to improve user experience and performance. Fragment Caching bridges the gap between no caching and the “ideal,” full-page caching. By extending the WordPress Transient API, W3TC allows developers to bring both horizontal and vertical scales to bear without doing anything differently.

Unlock dynamic Fragment Caching with W3 Total Cache Pro.

How do I implement fragment caching?

The W3 Total Cache Fragment caching layer adds new functionality to the WordPress Transients API:

  1. Adds support for grouping transients both per blog and sitewide
  2. Adds support for manual flushing of registered transient groups
  3. Adds support for action-based flushing of registered transient groups
  4. Adds support for caching filters and actions

To make the plugin aware that you’re grouping transients, you can use the following example as a guide:

add_action('w3tc_register_fragment_groups', 'my_plugin_register_groups');
function my_plugin_register_groups() {
//blog specific group and an array of actions that will trigger a flush of the group
w3tc_register_fragment_group('my_plugin_', array('publish_post'), 3600);
//If using MultiSite Network/site wide specific group and an array of actions that will trigger a flush of the group
w3tc_register_fragment_group_global('my_plugin_global_', array('edit_site'), 3600);
}
function my_plugin_flush_group() {
//manually flush a blog specific group.
w3tc_fragmentcache_flush_group('my_plugin_');
}
function my_plugin_flush_global_group() {
//manually flush a network wide group
w3tc_fragmentcache_flush_group('my_plugin_global_', true);
}
//Set transients
function on_some_event() {
if (false === get_transient('my_plugin_some_key'))
//my_plugin_ prefix is the group name we registered earlier
set_transient('my_plugin_some_key', 'blog specific value');
if (false === get_site_transient('my_plugin_some_key'))
//my_plugin_site_ prefix is the group name we registered earlier
set_site_transient('my_plugin_site_some_key', 'site wide specific value');
}
// Cache action example
add_action('theme_post_loop', 'cache_theme_post_loop_start',-999999999);
add_action('theme_post_loop', 'cache_theme_post_loop_end', 999999999);
/**
* Start outputbuffering
*/
function cache_theme_post_loop_start() {
w3tc_fragmentcache_start('example1', 'examples', 'theme_post_loop');
}
/**
* Store the output buffer .
*/
function cache_theme_post_loop_end() {
w3tc_fragmentcache_end('example1', 'examples', false);
}
// Cache filter example
add_filter('theme_filter', 'cache_theme_filter_start',-999999999);
add_filter('theme_filter', 'cache_theme_filter_end', 999999999);
/**
* Start filter buffering and return filter result
*/
function cache_theme_filter_start($data) {
return w3tc_fragmentcache_filter_start('example_filter1', 'examples', $hook, $data);
}
/**
* Store the filter result and return filter result.
*/
function cache_theme_filter_end($data) {
return w3tc_fragmentcache_filter_end('example_filter1', 'examples', $data);
}

It’s best to test on a page not visible to the public, or even better—a development environment. Just add define(‘W3TC_PRO_DEV_MODE’, true); to your wp-config.php file to test with Pro features on your development environment.

W3 Total Cache

You haven't seen fast until you've tried PRO

   Full Site CDN + Additional Caching Options
   Advanced Caching Statistics, Purge Logs and More

Everything you need to scale your WordPress Website and improve your PageSpeed.

Leave a Reply

Your email address will not be published. Required fields are marked *

W3 Total Cache logo

We Can Help Optimize Your Website

Optimize your site’s performance with our one-time premium services or ongoing monthly VIP support. View services

Got a Minute?

Your feedback helps us improve. Complete our customer survey now.

Partners

Bunny CDN Logo