Custom Cache Clearing Approach
- Custom Hooks and Webhooks:
- Since SiteGround does not provide specific hooks or filters for manually clearing the dynamic cache, you can create your own hooks in WordPress to trigger cache clearing.
- HTTP Requests to Clear Cache:
- SiteGround’s SG Optimizer plugin provides an API endpoint for clearing cache. You can make an HTTP request to this endpoint whenever a post or custom post type is updated.
Implementation Steps
1. Enable SG Optimizer API
First, ensure that the SG Optimizer API is enabled in the SG Optimizer plugin settings.
2. Create a Custom Function to Clear Cache
Add a custom function in your theme’s functions.php
file or a custom plugin to clear the cache using the SG Optimizer API endpoint.
function clear_siteground_cache() {
// Ensure the SG Optimizer API is enabled
if (class_exists('SG_CachePress_Supercacher')) {
SG_CachePress_Supercacher::purge_cache();
}
}
// Hook the custom function to WordPress actions
add_action('save_post', 'clear_siteground_cache'); // Trigger on post save
add_action('delete_post', 'clear_siteground_cache'); // Trigger on post delete
add_action('edit_post', 'clear_siteground_cache'); // Trigger on post edit
3. Ensure HTTP Requests are Authenticated
Make sure that the SG Optimizer plugin is configured to allow these API requests from your server. This can involve IP whitelisting or other security measures as per your hosting environment.
Alternative Approach Using External Tools
If direct integration with SG Optimizer’s API is not feasible, consider the following external solutions:
- Cron Jobs:
- Set up a cron job that periodically clears the cache. While this might not be immediate, it ensures that the cache is refreshed at regular intervals.
- WP-CLI Command:
- Use WP-CLI to clear the cache. This can be scheduled or triggered by an external script.
Example WP-CLI Command for Clearing Cache
- Create a Custom Script:
// custom-clear-cache.php
<?php
if (class_exists('SG_CachePress_Supercacher')) {
SG_CachePress_Supercacher::purge_cache();
}
?>
- Run the Script via WP-CLI:
wp eval-file custom-clear-cache.php
Implementing Cache Clearing on Content Update
To automatically trigger the cache clearing script on content update, you can create a custom webhook:
- Create a Custom Webhook Endpoint:
Add the following code to your functions.php
file or a custom plugin:
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/clear-cache', array(
'methods' => 'POST',
'callback' => 'clear_siteground_cache',
'permission_callback' => '__return_true',
));
});
function clear_siteground_cache() {
if (class_exists('SG_CachePress_Supercacher')) {
SG_CachePress_Supercacher::purge_cache();
return new WP_REST_Response('Cache cleared', 200);
} else {
return new WP_REST_Response('Cache clearing failed', 500);
}
}
- Trigger the Webhook on Content Update:
Add the following code to trigger the webhook on content update:
function trigger_clear_cache_webhook($post_ID, $post_after, $post_before) {
$response = wp_remote_post('https://your-site.com/wp-json/custom/v1/clear-cache');
return $post_ID;
}
add_action('save_post', 'trigger_clear_cache_webhook', 10, 3);
add_action('delete_post', 'trigger_clear_cache_webhook', 10, 3);
add_action('edit_post', 'trigger_clear_cache_webhook', 10, 3);
Summary
By implementing a custom solution using WordPress hooks and SG Optimizer’s API, or by setting up a cron job or WP-CLI command, you can effectively manage your dynamic cache and ensure your content updates are immediately reflected on the frontend. This approach provides a way to programmatically clear the cache, ensuring a smoother and more up-to-date user experience without manual intervention.