pre_unschedule_event
云策文档标注
概述
pre_unschedule_event 是一个 WordPress 过滤器,用于覆盖事件取消调度的默认过程。通过返回非空值,可以短路标准取消调度流程,并返回自定义值,适用于插件替换 wp-cron 的场景。
关键要点
- 过滤器名称:pre_unschedule_event,用于拦截事件取消调度操作。
- 返回值:返回非空值(如 true、false 或 WP_Error)将跳过默认取消调度,直接返回该值;返回 null 则继续正常流程。
- 参数:包括 $timestamp(Unix 时间戳)、$hook(动作钩子)、$args(回调参数)和 $wp_error(是否返回 WP_Error)。
- 适用场景:主要用于插件替换 wp-cron 系统时,自定义取消调度逻辑。
- 版本变化:WordPress 5.7.0 添加了 $wp_error 参数,允许返回 WP_Error 对象。
注意事项
- 确保返回值类型正确:成功时返回 true,失败时返回 false 或 WP_Error,以避免影响其他插件或核心功能。
- 谨慎使用短路功能:返回非空值会完全跳过默认取消调度,可能导致预期外的行为,需在插件中妥善处理。
原文内容
Filter to override unscheduling of events.
Description
Returning a non-null value will short-circuit the normal unscheduling process, causing the function to return the filtered value instead.
For plugins replacing wp-cron, return true if the event was successfully unscheduled, false or a WP_Error if not.
Parameters
$prenull|bool|WP_Error-
Value to return instead. Default null to continue unscheduling the event.
$timestampint-
Unix timestamp (UTC) for when to run the event.
$hookstring-
Action hook, the execution of which will be unscheduled.
$argsarray-
Arguments to pass to the hook’s callback function.
$wp_errorbool-
Whether to return a WP_Error on failure.
Source
$pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args, $wp_error );