钩子文档

schedule_event

💡 云策文档标注

概述

schedule_event 是一个 WordPress 过滤器钩子,用于在事件被添加到 cron 计划之前修改事件数据。它允许开发者拦截和调整计划事件,例如阻止特定事件执行或修改其参数。

关键要点

  • schedule_event 是一个过滤器钩子,应用于新事件添加到 cron 计划时。
  • 它接收一个参数:$event,这是一个包含事件数据的对象或 false(用于阻止事件被计划)。
  • 事件数据包括 hook(动作钩子)、timestamp(时间戳)、schedule(计划频率)、args(参数数组)和 interval(可选,间隔秒数)。
  • WordPress 核心在 3.21 版本中计划了多个重复事件和单次事件,如 wp_version_check 和 publish_future_post。

代码示例

add_filter( 'schedule_event', 'filter_cron_events', 10, 1);

function filter_cron_events($event) {
    switch ($event->hook) {
        case 'wp_version_check':
        case 'wp_update_plugins':
        case 'wp_update_themes':
            $event = false;
        break;
    }
    return $event;
}

📄 原文内容

Modify an event before it is scheduled.

Parameters

$eventobject|false
An object containing an event’s data, or boolean false to prevent the event from being scheduled.

  • hook string
    Action hook to execute when the event is run.
  • timestamp int
    Unix timestamp (UTC) for when to next run the event.
  • schedule string|false
    How often the event should subsequently recur.
  • args array
    Array containing each separate argument to pass to the hook’s callback function.
  • interval int
    Optional. The interval time in seconds for the schedule. Only present for recurring events.

More Information

  • The hook is applied when a new event is added to the cron schedule. The hook passes through one parameter: the $event being scheduled.
  • In WordPress 3.21, the following recurring events are scheduled by the core: wp_version_check, wp_update_plugins, wp_update_themes, wp_schedule_delete, and (for the main site of multisite installs only) wp_update_network_counts.
  • In WordPress 3.21, the following single events are scheduled on demand by the core: publish_future_post, do_pings, importer_scheduled_cleanup.

Source

$event = apply_filters( 'schedule_event', $event );

Changelog

Version Description
3.1.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example migrated from Codex:

    Eliminates all checks for plugins

    add_filter( 'schedule_event''filter_cron_events''10'1);
    
    function filter_cron_events($event) {
        switch ($event->hook) {
            case 'wp_version_check':
            case 'wp_update_plugins':
            case 'wp_update_themes':
            case 'wp_update_themes':
                $event = false;
            break;
        }
        return $event;
    }