cron_schedules
云策文档标注
概述
cron_schedules 是一个 WordPress 过滤器,用于添加自定义的 cron 时间间隔到非默认的 cron 计划中。它允许开发者扩展 WordPress 内置的 cron 调度选项,以便在插件或主题中定义自己的重复执行频率。
关键要点
- cron_schedules 过滤器接受一个数组参数 $schedules,用于添加或修改非默认的 cron 计划。
- 每个自定义计划需要包含 'interval'(以秒为单位的间隔时间)和 'display'(显示名称)两个键。
- 添加自定义计划时,必须将新计划合并到传入的 $schedules 数组中,以避免覆盖其他插件添加的计划。
- WordPress 默认提供 'hourly'、'twicedaily' 和 'daily' 等内置计划,此过滤器用于扩展这些选项。
代码示例
function my_add_weekly( $schedules ) {
$schedules['weekly'] = array(
'interval' => 604800,
'display' => __('Once Weekly')
);
return $schedules;
}
add_filter( 'cron_schedules', 'my_add_weekly' );注意事项
- 在添加自定义计划时,建议在主要动作(如 init 或 wp_loaded)中注册 cron_schedules 过滤器,以确保在使用 wp_schedule_event 之前计划已存在。
- 避免直接返回仅包含自定义计划的数组,应使用数组合并操作来保留其他插件添加的计划。
原文内容
Filters the non-default cron schedules.
Parameters
$new_schedulesarray-
An array of non-default cron schedules keyed by the schedule name. Default empty array.
...$0arrayCron schedule information.intervalintThe schedule interval in seconds.displaystringThe schedule display name.
Source
return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );Changelog
Version Description 2.1.0 Introduced. User Contributed Notes
You must log in before being able to contribute a note or feedback.
Skip to note 5 content
Dominik Schilling
Add a custom cron schedule for every 5 minutes:
/** * Adds a custom cron schedule for every 5 minutes. * * @param array $schedules An array of non-default cron schedules. * @return array Filtered array of non-default cron schedules. */ function devhub_custom_cron_schedule( $schedules ) { $schedules[ 'every-5-minutes' ] = array( 'interval' => 5 * MINUTE_IN_SECONDS, 'display' => __( 'Every 5 minutes', 'devhub' ) ); return $schedules; } add_filter( 'cron_schedules', 'devhub_custom_cron_schedule' );Skip to note 6 content
dhl17
custom cron schedule for every 10 & 15 Seconds
function custom_cron_job_recurrence( $schedules ) { if(!isset($schedules['10sec'])) { $schedules['10sec'] = array( 'display' => __( 'Every 10 Seconds', 'twentyfifteen' ), 'interval' => 10, ); } if(!isset($schedules['15sec'])) { $schedules['15sec'] = array( 'display' => __( 'Every 15 Seconds', 'twentyfifteen' ), 'interval' => 15, ); } return $schedules; } add_filter( ‘cron_schedules’, ‘custom_cron_job_recurrence’ );Skip to note 7 content
Ihtisham Zahoor
Add a user-defined interval value:
Using functinal approach
add_filter( 'cron_schedules', 'wpdocs_add_cron_interval' ); function wpdocs_add_cron_interval( $schedules ) { $options = get_option( 'wpdocs_custom_interval' ); $interval = ( ! empty( $options ) ? absint( $options ) * HOUR_IN_SECONDS : DAY_IN_SECONDS ); $schedules['wpdocs_interval'] = array( 'interval' => $interval, 'display' => esc_html__( 'Custom Interval' ) ); return $schedules; }Using class based approach
add_filter( 'cron_schedules', array( $this, 'add_cron_interval' ) ); /** * Adds custom time intervals for the cron. */ private function add_cron_interval( $schedules ) { $options = get_option( 'wpdocs_custom_interval' ); $interval = ( ! empty( $options ) ? absint( $options ) * HOUR_IN_SECONDS : DAY_IN_SECONDS ); $schedules['wpdocs_interval'] = array( 'interval' => $interval, 'display' => esc_html__( 'Custom Interval' ) ); return $schedules; }Skip to note 8 content
BogdanFix
As a handy tip to save your time, I’d suggest to always hook to
cron_schedulesfilter inside a major action that runs on each page load. Likeinitorwp_loaded. And also hook to that filter before your code using the custom schedule.For example:
// function that registers new custom schedule function bf_add_custom_schedule( $schedules ) { $schedules[ 'every_five_minutes' ] = array( 'interval' => 300, 'display' => 'Every 5 minutes', ); return $schedules; } // function that schedules custom event function bf_schedule_custom_event() { // the actual hook to register new custom schedule add_filter( 'cron_schedules', 'bf_add_custom_schedule' ); // schedule custom event if( !wp_next_scheduled( 'bf_your_custom_event' ) ) { wp_schedule_event( time(), 'every_five_minutes', 'bf_your_custom_event' ); } } add_action( 'init', 'bf_schedule_custom_event' ); // fire custom event function bf_do_something_on_schedule() { // your code... } add_action( 'bf_your_custom_event', 'bf_do_something_on_schedule' );This approach guarantees that your custom schedule is always registered before it is used in
wp_schedule_eventfunction. And saves you from nasty"invalid_schedule":"Event schedule does not exist."error.Cheers 🙂