wp_get_schedules() WordPress Function

The wp_get_schedules() function allows you to retrieve the schedule of events for a given time period. The function takes two arguments: the first is the start date and the second is the end date. The function returns an associative array of events, with the keys being the event IDs and the values being the event objects.

wp_get_schedules() #

Retrieve supported event recurrence schedules.


Description

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 easily add your schedule by doing the following.

// Filter parameter variable name is 'array'.
$array['monthly'] = array(
    'interval' => MONTH_IN_SECONDS,
    'display'  => __( 'Once Monthly' )
);

Top ↑

Return

(array[])


Top ↑

More Information

Example Return Values:

Array
(
   [hourly] => Array
       (
           [interval] => 3600
           [display] => Once Hourly
       )
   [twicedaily] => Array
       (
           [interval] => 43200
           [display] => Twice Daily
       )
   [daily] => Array
       (
           [interval] => 86400
           [display] => Once Daily
       )
)

Top ↑

Source

File: wp-includes/cron.php

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 schedule arrays. Default empty.
	 */
	return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
}


Top ↑

Changelog

Changelog
VersionDescription
5.4.0The 'weekly' schedule was added.
2.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.