wp_get_schedules()
Retrieve supported event recurrence schedules.
The default supported recurrences are 'hourly', 'twicedaily', 'daily', and 'weekly'. A plugin may add more by hooking into the cron_schedules filter. The filter accepts an array of arrays. The outer array has a key that is the name of the schedule, for example 'monthly'. The value is an array with two keys, one is 'interval' and the other is 'display'.
The 'interval' is a number in seconds of when the cron job should run. So for 'hourly' the time is HOUR_IN_SECONDS (60 * 60 or 3600). For 'monthly', the value would be MONTH_IN_SECONDS (30 * 24 * 60 * 60 or 2592000).
The 'display' is the description. For the 'monthly' key, the 'display' would be __('Once Monthly').
For your plugin, you will be passed an array. You can add your schedule by doing the following:
// Filter parameter variable name is 'array'. $array['monthly'] = array( 'interval' => MONTH_IN_SECONDS, 'display' => __( 'Once Monthly' ) );
Hooks from the function
Return
Array
. The array of cron schedules keyed by the schedule name.
Usage
wp_get_schedules();
Examples
#1 An example of what the function returns:
$rrr = wp_get_schedules(); echo print_r($rrr); /* we get: Array ( [hourly] => Array ( [interval] => 3600 [display] => Every hour ) [twicedaily] => Array ( [interval] => 43200 [display] => Twice a day ) [daily] => Array ( [interval] => 86400 [display] => Every day ) ) */
#2 Example of adding a new interval for a plugin.
To add a new interval for Cron in WordPress we can use the filter cron_schedules. Let's add an event recurrence interval: once a week:
add_filter( 'cron_schedules', 'cron_add_weekly' ); function cron_add_weekly( $schedules ) { // Adds once weekly to the existing schedules. $schedules['weekly'] = array( 'interval' => WEEK_IN_SECONDS, // 604800 sec. 'display' => __( 'Once Weekly' ) ); return $schedules; }
Changelog
Since 2.1.0 | Introduced. |
Since 5.4.0 | The 'weekly' schedule was added. |