Purge LiteSpeed Cache on the Homepage daily Mon-Fri

Hi,

We needed a small piece of code, that would purge the Homepage Monday through Friday selectively, at 12:01pm CET. Please note: This code uses the Berlin Timezone at 12:01pm CET. Change according to your needs. Insert into the functions.php, f.ex. through Code Snippets.

// Schedule the purge event if it's not already scheduled
if (!wp_next_scheduled('litespeed_purge_homepage_daily')) {
    wp_schedule_event(strtotime('today 12:01:00'), 'litespeed_purge_homepage_schedule', 'litespeed_purge_homepage_daily');
}

// Custom schedule for Monday to Friday at 12:01 PM Europe/Berlin time
function litespeed_purge_homepage_cron_schedules($schedules) {
    $schedules['litespeed_purge_homepage_schedule'] = array(
        'interval' => 86400, // 24 hours in seconds
        'display' => __('Daily at 12:01 PM Europe/Berlin time (Mon-Fri)')
    );
    return $schedules;
}
add_filter('cron_schedules', 'litespeed_purge_homepage_cron_schedules');

// Function to purge homepage cache
function litespeed_purge_homepage_daily() {
    // Check if it's Monday to Friday
    if (date('N') <= 5) {
        // Purge homepage cache
        do_action('litespeed_purge_url', home_url());
    }
}
add_action('litespeed_purge_homepage_daily', 'litespeed_purge_homepage_daily');

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *