wp_get_schedules()
Gets the supported Cron time intervals.
Supported time intervals are the registered intervals in WP that can be specified in event scheduling functions, such as wp_schedule_event() - in the $recurrence argument we specify: 'hourly', 'twicedaily' or 'daily'.
Plugins can add their own intervals using the cron_schedules filter. This hook passes an array of arrays with data about each interval (see example 1): the key names of the main array are the names of the intervals, and the value is an array with two elements: 'interval' and 'display'.
interval is the value in seconds, the interval after which the task should repeat. Thus, hourly (every hour) has a value of - 3600 (60*60). The weekly interval will accordingly have a value of 60*60*24*7 or 604800.
Hooks from the function
Returns
Array. A two-dimensional associative array of data.
Usage
$schedules = 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. |