钩子文档

pre_unschedule_hook

💡 云策文档标注

概述

pre_unschedule_hook 是一个 WordPress 过滤器,用于在取消调度所有与指定钩子关联的事件时进行拦截和自定义处理。开发者可以通过此过滤器覆盖默认的取消调度逻辑,返回自定义值以控制函数行为。

关键要点

  • 过滤器名称:pre_unschedule_hook,用于在 wp_unschedule_hook() 函数执行前拦截。
  • 返回非 null 值会短路正常取消调度过程,函数直接返回过滤后的值。
  • 参数包括 $pre(默认 null)、$hook(钩子名称)和 $wp_error(是否在失败时返回 WP_Error)。
  • 适用于替换 wp-cron 的插件,应返回成功取消调度的事件数或错误对象。

代码示例

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

注意事项

  • 从 WordPress 5.7.0 开始,新增 $wp_error 参数,允许返回 WP_Error 对象。
  • 如果取消调度失败,根据 $wp_error 参数返回 false 或 WP_Error。

📄 原文内容

Filter to override clearing all events attached to the 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). If unscheduling one or more events fails then return either a WP_Error object or false depending on the value of the $wp_error parameter.

Parameters

$prenull|int|false|WP_Error
Value to return instead. Default null to continue unscheduling the hook.
$hookstring
Action hook, the execution of which will be unscheduled.
$wp_errorbool
Whether to return a WP_Error on failure.

Source

$pre = apply_filters( 'pre_unschedule_hook', null, $hook, $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.