wp_get_schedules()WP 2.1.0

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.

1 time — 0.000001 sec (speed of light) | 50000 times — 0.91 sec (very fast) | PHP 7.0.19, WP 5.0.2
Hooks from the function

Returns

Array. A two-dimensional associative array of data.

Usage

$schedules = wp_get_schedules();

Examples

0

#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
		)

)
*/
0

#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.

wp_get_schedules() code WP 6.8.3

function wp_get_schedules() {
	$schedules = array(
		'hourly'     => array(
			'interval' => HOUR_IN_SECONDS,
			'display'  => __( 'Once Hourly' ),
		),
		'twicedaily' => array(
			'interval' => 12 * HOUR_IN_SECONDS,
			'display'  => __( 'Twice Daily' ),
		),
		'daily'      => array(
			'interval' => DAY_IN_SECONDS,
			'display'  => __( 'Once Daily' ),
		),
		'weekly'     => array(
			'interval' => WEEK_IN_SECONDS,
			'display'  => __( 'Once Weekly' ),
		),
	);

	/**
	 * Filters the non-default cron schedules.
	 *
	 * @since 2.1.0
	 *
	 * @param array $new_schedules {
	 *     An array of non-default cron schedules keyed by the schedule name. Default empty array.
	 *
	 *     @type array ...$0 {
	 *         Cron schedule information.
	 *
	 *         @type int    $interval The schedule interval in seconds.
	 *         @type string $display  The schedule display name.
	 *     }
	 * }
	 */
	return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
}