How to Clear Cache on WordPress

In a project, we needed to clear all cache while saving a custom post type, but how we didn’t know that cache plugin the customer going to use then we did a script that works in the most popular cache plugins.

We use the save_post  hook to do the script, but it too works in init  hook.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
if( ! function_exists('letsgo_clear_cache') ) {
function letsgo_clear_cache() {
// WP Rocket
if ( function_exists( 'rocket_clean_domain' ) ) {
rocket_clean_domain();
}
// W3 Total Cache : w3tc
if ( function_exists( 'w3tc_pgcache_flush' ) ) {
w3tc_pgcache_flush();
}
// WP Super Cache : wp-super-cache
if ( function_exists( 'wp_cache_clear_cache' ) ) {
wp_cache_clear_cache();
}
// WP Fastest Cache
if( function_exists('wpfc_clear_all_cache') ) {
wpfc_clear_all_cache(true);
}
// WPEngine
if ( class_exists( 'WpeCommon' ) && method_exists( 'WpeCommon', 'purge_memcached' ) ) {
WpeCommon::purge_memcached();
WpeCommon::purge_varnish_cache();
}
// SG Optimizer by Siteground
if ( function_exists( 'sg_cachepress_purge_cache' ) ) {
sg_cachepress_purge_cache();
}
// LiteSpeed
if( class_exists('LiteSpeed_Cache_API') && method_exists('LiteSpeed_Cache_API', 'purge_all') ) {
LiteSpeed_Cache_API::purge_all();
}
// Cache Enabler
if( class_exists('Cache_Enabler') && method_exists('Cache_Enabler', 'clear_total_cache') ) {
Cache_Enabler::clear_total_cache();
}
// Pagely
if ( class_exists('PagelyCachePurge') && method_exists('PagelyCachePurge','purgeAll') ) {
PagelyCachePurge::purgeAll();
}
// Autoptimize
if( class_exists('autoptimizeCache') && method_exists( 'autoptimizeCache', 'clearall') ) {
autoptimizeCache::clearall();
}
// Comet cache
if( class_exists('comet_cache') && method_exists('comet_cache', 'clear') ) {
comet_cache::clear();
}
// Hummingbird Cache
if( class_exists('\Hummingbird\WP_Hummingbird') &&
method_exists('\Hummingbird\WP_Hummingbird', 'flush_cache')
) {
\Hummingbird\WP_Hummingbird::flush_cache();
}
}
add_action( 'save_post', 'letsgo_clear_cache' );
}
<?php if( ! function_exists('letsgo_clear_cache') ) { function letsgo_clear_cache() { // WP Rocket if ( function_exists( 'rocket_clean_domain' ) ) { rocket_clean_domain(); } // W3 Total Cache : w3tc if ( function_exists( 'w3tc_pgcache_flush' ) ) { w3tc_pgcache_flush(); } // WP Super Cache : wp-super-cache if ( function_exists( 'wp_cache_clear_cache' ) ) { wp_cache_clear_cache(); } // WP Fastest Cache if( function_exists('wpfc_clear_all_cache') ) { wpfc_clear_all_cache(true); } // WPEngine if ( class_exists( 'WpeCommon' ) && method_exists( 'WpeCommon', 'purge_memcached' ) ) { WpeCommon::purge_memcached(); WpeCommon::purge_varnish_cache(); } // SG Optimizer by Siteground if ( function_exists( 'sg_cachepress_purge_cache' ) ) { sg_cachepress_purge_cache(); } // LiteSpeed if( class_exists('LiteSpeed_Cache_API') && method_exists('LiteSpeed_Cache_API', 'purge_all') ) { LiteSpeed_Cache_API::purge_all(); } // Cache Enabler if( class_exists('Cache_Enabler') && method_exists('Cache_Enabler', 'clear_total_cache') ) { Cache_Enabler::clear_total_cache(); } // Pagely if ( class_exists('PagelyCachePurge') && method_exists('PagelyCachePurge','purgeAll') ) { PagelyCachePurge::purgeAll(); } // Autoptimize if( class_exists('autoptimizeCache') && method_exists( 'autoptimizeCache', 'clearall') ) { autoptimizeCache::clearall(); } // Comet cache if( class_exists('comet_cache') && method_exists('comet_cache', 'clear') ) { comet_cache::clear(); } // Hummingbird Cache if( class_exists('\Hummingbird\WP_Hummingbird') && method_exists('\Hummingbird\WP_Hummingbird', 'flush_cache') ) { \Hummingbird\WP_Hummingbird::flush_cache(); } } add_action( 'save_post', 'letsgo_clear_cache' ); }
<?php

if( ! function_exists('letsgo_clear_cache') ) {

  function letsgo_clear_cache() {
    
    // WP Rocket
    if ( function_exists( 'rocket_clean_domain' ) ) {
      rocket_clean_domain();
    }
    // W3 Total Cache : w3tc
    if ( function_exists( 'w3tc_pgcache_flush' ) ) {
      w3tc_pgcache_flush();
    }
    // WP Super Cache : wp-super-cache
    if ( function_exists( 'wp_cache_clear_cache' ) ) {
      wp_cache_clear_cache();
    }
    // WP Fastest Cache
    if( function_exists('wpfc_clear_all_cache') ) {
      wpfc_clear_all_cache(true);
    }
    // WPEngine
    if ( class_exists( 'WpeCommon' ) && method_exists( 'WpeCommon', 'purge_memcached' ) ) {
      WpeCommon::purge_memcached();
      WpeCommon::purge_varnish_cache();
    }
    // SG Optimizer by Siteground
    if ( function_exists( 'sg_cachepress_purge_cache' ) ) {
      sg_cachepress_purge_cache();
    }
    // LiteSpeed
    if( class_exists('LiteSpeed_Cache_API') && method_exists('LiteSpeed_Cache_API', 'purge_all') ) {
      LiteSpeed_Cache_API::purge_all();
    }
    // Cache Enabler
    if( class_exists('Cache_Enabler') && method_exists('Cache_Enabler', 'clear_total_cache') ) {
      Cache_Enabler::clear_total_cache();
    }
    // Pagely
    if ( class_exists('PagelyCachePurge') && method_exists('PagelyCachePurge','purgeAll') ) {
      PagelyCachePurge::purgeAll();
    }
    // Autoptimize
    if( class_exists('autoptimizeCache') && method_exists( 'autoptimizeCache', 'clearall') ) {
      autoptimizeCache::clearall();
    }
    // Comet cache
    if( class_exists('comet_cache') && method_exists('comet_cache', 'clear') ) {
      comet_cache::clear();
    }
    // Hummingbird Cache
    if( class_exists('\Hummingbird\WP_Hummingbird') &&
      method_exists('\Hummingbird\WP_Hummingbird', 'flush_cache')
    ) {
      \Hummingbird\WP_Hummingbird::flush_cache();
    }
  }

  add_action( 'save_post', 'letsgo_clear_cache' );
}

 

Use it wisely 🙂

0 0 votes
Article Rating
Subscribe
Notify of
guest


0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to Top
0
Would love your thoughts, please comment.x
()
x