Caching With Widgets

W3 Total Cache is a great WordPress plugin that will help your Google Page Speed score and will also speed up your website.  The only problem is that it doesn’t clear the cache when widgets are updated.  So for example if you have a custom theme with a widget on your page and you update this widget, changes are not going to be seen.  You’ll have to empty the cache on the W3 Total Cache page.

WPEngine also has a caching system that doesn’t clear the cache upon editing widgets.  This means that every time you update a widget you have to manually clear the cache.  WPEngine is a great WordPress host, but doing something manually isn’t good for clients.

A solution is to add a function to clear the cache every time the widget is updated.  The code below covers W3 Total Cache, WP Super Cache and WPEngine.  You can copy-paste it into your functions.php or just install the Clear Cache For Widgets plugin.

function clear_cache_for_widgets( $instance ) {

// if W3 Total Cache is being used, clear the cache
if ( function_exists( 'w3tc_pgcache_flush' ) ) {
w3tc_pgcache_flush();
}
// if WP Super Cache is being used, clear the cache
else if ( function_exists( 'wp_cache_clean_cache' ) ) {
global $file_prefix;
wp_cache_clean_cache( $file_prefix );
}
else if ( class_exists( 'WpeCommon' ) ) {
WpeCommon::purge_memcached();
WpeCommon::clear_maxcdn_cache();
WpeCommon::purge_varnish_cache();
}

return $instance;
}
add_filter( 'widget_update_callback', 'clear_cache_for_widgets', 10 );

Source: http://scratch99.com/wordpress/development/clearing-cache-when-widget-saved/
http://wordpress.stackexchange.com/questions/25425/page-cache-for-categories-not-updating-with-w3-total-cache

Tagged with: , , , ,

Leave a Reply