钩子文档

pre_clear_scheduled_hook

💡 云策文档标注

概述

pre_clear_scheduled_hook 是一个 WordPress 过滤器,用于覆盖清除计划钩子的默认行为。通过返回非空值,可以短路正常的取消调度过程,并返回过滤后的值。

关键要点

  • 过滤器名称:pre_clear_scheduled_hook
  • 作用:允许插件或主题在清除计划钩子时自定义返回值,例如用于替换 wp-cron 的系统
  • 参数:$pre(默认 null)、$hook(钩子名称)、$args(参数数组)、$wp_error(是否返回 WP_Error)
  • 返回值:可返回整数(成功取消的事件数)、false、WP_Error 或 null(继续正常处理)

代码示例

$pre = apply_filters( 'pre_clear_scheduled_hook', null, $hook, $args, $wp_error );

注意事项

  • 从 WordPress 5.7.0 开始,新增 $wp_error 参数,并支持返回 WP_Error 对象
  • 从 WordPress 5.1.0 引入此过滤器
  • 相关函数:wp_clear_scheduled_hook() 用于取消调度所有指定钩子的事件

📄 原文内容

Filter to override clearing a scheduled hook.

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 the number of events successfully unscheduled (zero if no events were registered with the hook) or false or a WP_Error if unscheduling one or more events fails.

Parameters

$prenull|int|false|WP_Error
Value to return instead. Default null to continue unscheduling 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_clear_scheduled_hook', null, $hook, $args, $wp_error );

Changelog

Version Description
5.7.0 The $wp_error parameter was added, and a WP_Error object can now be returned.
5.1.0 Introduced.