插件开发文档

💡 云策文档标注

概述

WP-Cron 是 WordPress 用于处理基于时间任务调度的核心机制,支持核心功能如检查更新和发布定时文章。它通过页面加载触发任务执行,而非持续运行,可能导致调度延迟。

关键要点

  • WP-Cron 基于页面加载检查并运行计划任务,不同于系统 cron 的持续运行。
  • 适用于共享主机环境,提供比系统调度器更简单的 API 来设置任务。
  • 任务被放入队列,确保最终执行,但执行时间可能不精确。

📄 原文内容

What is WP-Cron

WP-Cron is how WordPress handles scheduling time-based tasks in WordPress. Several WordPress core features, such as checking for updates and publishing scheduled post, utilize WP-Cron. The “Cron” part of the name comes from the cron time-based task scheduling system that is available on UNIX systems.

WP-Cron works by checking, on every page load, a list of scheduled tasks to see what needs to be run. Any tasks due to run will be called during that page load.

WP-Cron does not run constantly as the system cron does; it is only triggered on page load.

Scheduling errors could occur if you schedule a task for 2:00PM and no page loads occur until 5:00PM.

Why use WP-Cron

  • WordPress core and many plugins need a scheduling system to perform time-based tasks. However, many hosting services are shared and do not provide access to the system scheduler.
  • Using the WordPress API is a simpler method for setting scheduled tasks than going outside of WordPress to the system scheduler.
  • With the system scheduler, if the time passes and the task did not run, it will not be re-attempted. With WP-Cron, all scheduled tasks are put into a queue and will run at the next opportunity (meaning the next page load). So while you can’t be 100% sure when your task will run, you can be 100% sure that it will run eventually.