Customizing Cron Schedules
This guide explains how to use the wp_statistics_cron_schedules
filter to modify the predefined cron schedules used in the WP Statistics plugin. By leveraging this filter, you can customize the intervals, display names, start and end dates, and next schedule times of the cron jobs.
Applying the wp_statistics_cron_schedules
Filter
Purpose
The wp_statistics_cron_schedules
filter allows developers to adjust the predefined scheduling intervals for WP Statistics. This filter is applied to the array of schedules generated by the getSchedules
method.
Hook Syntax
apply_filters('wp_statistics_cron_schedules', $schedules);
Parameters
$schedules
(array): An associative array of predefined schedules. Each schedule includes:interval
(int): The interval in seconds between each scheduled event.display
(string): The display name of the schedule.start
(string): The start date of the schedule period in ‘Y-m-d’ format.end
(string): The end date of the schedule period in ‘Y-m-d’ format.next_schedule
(int): The Unix timestamp for the next scheduled occurrence.
Default Schedules
The default schedules passed to the filter are:
Daily Schedule
- Interval:
DAY_IN_SECONDS
(24 hours). - Display: “Daily”.
- Start: Yesterday’s date.
- End: Today’s date.
- Next Schedule: Tomorrow at 08:00 AM.
Weekly Schedule
- Interval:
WEEK_IN_SECONDS
(7 days). - Display: “Weekly”.
- Start: 7 days ago.
- End: Today’s date.
- Next Schedule: The next occurrence of the start day of the week at 08:00 AM.
Bi-Weekly Schedule
- Interval:
2 * WEEK_IN_SECONDS
(14 days). - Display: “Bi-Weekly”.
- Start: 14 days ago.
- End: Today’s date.
- Next Schedule: The second occurrence of the start day of the week at 08:00 AM.
Monthly Schedule
- Interval:
MONTH_IN_SECONDS
. - Display: “Monthly”.
- Start: 30 days ago.
- End: Today’s date.
- Next Schedule: The first day of the next month at 08:00 AM.
Example Usage
To modify the existing schedules or add a new custom schedule, hook into the wp_statistics_cron_schedules
filter in your theme or plugin:
add_filter('wp_statistics_cron_schedules', 'custom_wp_statistics_cron_schedules');
function custom_wp_statistics_cron_schedules($schedules) {
// Example of modifying the daily schedule interval
$schedules['daily']['interval'] = 12 * HOUR_IN_SECONDS; // Every 12 hours
$schedules['daily']['display'] = __('Twice Daily', 'your-text-domain');
// Example of adding a custom schedule
$schedules['hourly'] = [
'interval' => HOUR_IN_SECONDS,
'display' => __('Hourly', 'your-text-domain'),
'start' => wp_date('Y-m-d', strtotime("-1 hour")),
'end' => wp_date('Y-m-d'),
'next_schedule' => strtotime('+1 hour')
];
return $schedules;
}
In this example:
- The interval of the daily schedule is modified to run every 12 hours.
- A new custom schedule named “hourly” is added, which runs every hour.