Last active
October 11, 2017 15:33
-
-
Save jtsternberg/626da1796c59427893778d265ea7943f to your computer and use it in GitHub Desktop.
Purge siteground's cache when updating content on the frontend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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