cron_schedules WordPress Filter Hook

The cron_schedules hook allows you to modify the cron schedule in WordPress. This can be useful for scheduling cron jobs to run more or less often than the default schedule.

apply_filters( 'cron_schedules', array[] $new_schedules ) #

Filters the non-default cron schedules.


Parameters

$new_schedules

(array[])An array of non-default cron schedule arrays. Default empty.


Top ↑

More Information

The filter accepts an array of non-default cron schedules in arrays (an array of arrays). The outer array has a key that is the name of the schedule (for example, ‘weekly’). 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 shall run. So, for a hourly schedule, the ‘interval’ value would be 3600 or 60*60. For for a weekly schedule, the ‘interval’ value would be 60*60*24*7 or 604800.

The ‘display’ is the description of the non-default cron schedules. For the ‘weekly’ key, the ‘display’ may be __(‘Once Weekly’).

Why is this important?

When scheduling your own actions to run using the WordPress Cron service, you have to specify which interval WordPress should use. WordPress has its own, limited, default set of intervals, or “schedules”, including ‘hourly’, ‘twicedaily’, and ‘daily’. This filter allows you to add your own intervals to the default set.

For your plugin, you will be passed an array, you can easily add a weekly schedule by doing something like:

function my_add_weekly( $schedules ) {
	// add a 'weekly' schedule to the existing set
	$schedules['weekly'] = array(
		'interval' => 604800,
		'display' => __('Once Weekly')
	);
	return $schedules;
}
add_filter( 'cron_schedules', 'my_add_weekly' ); 

Adding multiple intervals works similarly:

function my_add_intervals($schedules) {
	// add a 'weekly' interval
	$schedules['weekly'] = array(
		'interval' => 604800,
		'display' => __('Once Weekly')
	);
	$schedules['monthly'] = array(
		'interval' => 2635200,
		'display' => __('Once a month')
	);
	return $schedules;
}
add_filter( 'cron_schedules', 'my_add_intervals'); 

Be sure to add your schedule to the passed array, as shown in the example. If you simply return only your own schedule array then you will potentially delete schedules created by other plugins.


Top ↑

Source

File: wp-includes/cron.php

View on Trac



Top ↑

Changelog

Changelog
VersionDescription
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.