Skip to content

Instantly share code, notes, and snippets.

@jekayode
Created July 2, 2024 06:18
Show Gist options
  • Select an option

  • Save jekayode/186aad7f0e28a8aebf65cd8a087436be to your computer and use it in GitHub Desktop.

Select an option

Save jekayode/186aad7f0e28a8aebf65cd8a087436be to your computer and use it in GitHub Desktop.
A PHP script to be included in the functions.php file of a WordPress theme. This script schedules daily events to disable WP Rocket caching at 10:58 AM and re-enable it at 11:30 AM, including a cache cleanup. It ensures cache control actions are performed automatically at specified times, accommodating frequent updates via the Custom API.
<?php
// Schedule the 'rocket_is_importing' filter to be added at 10:58 AM daily
add_action('init', 'schedule_cache_control');
function schedule_cache_control() {
if (!wp_next_scheduled('add_importing_filter_event')) {
wp_schedule_event(strtotime('10:58:00'), 'daily', 'add_importing_filter_event');
}
}
add_action('add_importing_filter_event', 'add_importing_filter');
function add_importing_filter() {
add_filter('rocket_is_importing', '__return_true');
// Assuming import takes 30 minutes; schedule removal of the filter
if (!wp_next_scheduled('remove_importing_filter_event')) {
wp_schedule_single_event(time() + 1800, 'remove_importing_filter_event'); // 1800 seconds = 30 minutes
}
}
// Hook to remove the filter after import is completed (assuming import completion hook is 'import_complete_hook')
add_action('import_complete_hook', 'remove_importing_filter');
add_action('remove_importing_filter_event', 'remove_importing_filter');
function remove_importing_filter() {
remove_filter('rocket_is_importing', '__return_true');
if (function_exists('rocket_clean_domain')) {
rocket_clean_domain();
}
}
// Ensure scheduled events are set up
schedule_cache_control();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment