pre_schedule_event
概述
pre_schedule_event 是一个 WordPress 过滤器,用于在调度事件(如 cron 任务)时进行拦截和自定义处理。通过返回非空值,可以阻止事件被添加到 cron 数组,并返回过滤后的值。
关键要点
- 过滤器用于覆盖事件调度,返回非空值(如 true、false 或 WP_Error)会短路 cron 添加过程。
- 适用于单次事件(schedule 为 false)和重复事件(schedule 为 wp_get_schedules() 中的值,interval 为整数间隔)。
- 插件替换 wp-cron 时,建议检查十分钟内的相同事件,并使用 'schedule_event' 过滤器验证其他插件是否已禁止该事件。
- 参数包括 $result(默认 null)、$event(包含 hook、timestamp、schedule、args、interval 的对象)和 $wp_error(控制是否返回 WP_Error)。
- 返回 true 表示事件已调度,false 或 WP_Error 表示失败。
注意事项
- 从 WordPress 5.7.0 开始,添加了 $wp_error 参数,并支持返回 WP_Error 对象。
- 此过滤器在 WordPress 5.1.0 中引入。
Filter to override scheduling an event.
Description
Returning a non-null value will short-circuit adding the event to the cron array, causing the function to return the filtered value instead.
Both single events and recurring events are passed through this filter; single events have $event->schedule as false, whereas recurring events have this set to a recurrence from wp_get_schedules() . Recurring events also have the integer recurrence interval set as $event->interval.
For plugins replacing wp-cron, it is recommended you check for an identical event within ten minutes and apply the ‘schedule_event’ filter to check if another plugin has disallowed the event before scheduling.
Return true if the event was scheduled, false or a WP_Error if not.
Parameters
$resultnull|bool|WP_Error-
The value to return instead. Default null to continue adding the event.
$eventobject-
An object containing an event’s data.
hookstringAction hook to execute when the event is run.timestampintUnix timestamp (UTC) for when to next run the event.schedulestring|falseHow often the event should subsequently recur.argsarrayArray containing each separate argument to pass to the hook’s callback function.intervalintOptional. The interval time in seconds for the schedule. Only present for recurring events.
$wp_errorbool-
Whether to return a WP_Error on failure.
Source
$pre = apply_filters( 'pre_schedule_event', null, $event, $wp_error );