Skip to content

Instantly share code, notes, and snippets.

@knolaust
Last active January 30, 2024 14:50
Show Gist options
  • Save knolaust/c66c1b83bce7ff3a009f32adbb1ba83a to your computer and use it in GitHub Desktop.
Save knolaust/c66c1b83bce7ff3a009f32adbb1ba83a to your computer and use it in GitHub Desktop.
Clear Siteground cache for site or for single page
<?php
/**
* Clears the SiteGround cache for a specific page or the entire site.
*
* This function checks if a valid URL is provided and clears the cache
* for that specific page. If the URL is invalid or not provided, it clears
* the cache for the entire site. It relies on the 'sg_cachepress_purge_cache' function
* provided by SiteGround's caching functionality.
*
* Gist Keywords: wordpress, cache, performance, siteground
*
* @param string $page_url Optional. The URL of the page to clear the cache for.
* If empty or invalid, the entire cache is cleared.
* @return void
* @author Knol Aust
*/
function knolaust_clear_sg_cache($page_url = ''){
if (function_exists('sg_cachepress_purge_cache')) {
if (!empty($page_url) && filter_var($page_url, FILTER_VALIDATE_URL)) {
sg_cachepress_purge_cache($page_url);
} else {
sg_cachepress_purge_cache();
}
}
}
/** Instructions */
// Usage of knolaust_clear_sg_cache function
// 1. To clear the cache for a specific page:
// Example: knolaust_clear_sg_cache('https://www.example.com/specific-page/');
// 2. To clear the entire site's cache:
// Example: knolaust_clear_sg_cache();
// Notes:
// - The function checks if the provided URL is valid. If the URL is invalid or not provided,
// the cache for the entire site is cleared.
// - Ensure that the 'sg_cachepress_purge_cache' function is available in your environment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment