Skip to content

Instantly share code, notes, and snippets.

@jtsternberg
Last active October 11, 2017 15:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtsternberg/626da1796c59427893778d265ea7943f to your computer and use it in GitHub Desktop.
Save jtsternberg/626da1796c59427893778d265ea7943f to your computer and use it in GitHub Desktop.
Purge siteground's cache when updating content on the frontend
<?php
/**
* Trigger a siteground cache purge when saving through a frontend form.
*/
function naspd_clear_cache_when_saving_frontend( $post_id, $post, $update ) {
global $naspd_clear_sg_cache;
if ( ! is_admin() && $update && is_callable( array( 'SG_CachePress_Supercacher', 'purge_cache' ) ) ) {
$naspd_clear_sg_cache = true;
}
}
add_action( 'save_post', 'naspd_clear_cache_when_saving_frontend', 10, 3 );
/**
* Purge the cache at the end of our the page load, and only one time.
*/
function naspd_maybe_clear_cache_after_save() {
global $naspd_clear_sg_cache;
if ( defined( 'DOING_AJAX' ) ) {
return;
}
if ( $naspd_clear_sg_cache && is_callable( array( 'SG_CachePress_Supercacher', 'purge_cache' ) ) {
SG_CachePress_Supercacher::purge_cache();
}
}
add_action( 'shutdown', 'naspd_maybe_clear_cache_after_save' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment