函数文档

wp_doing_cron()

💡 云策文档标注

概述

wp_doing_cron() 函数用于判断当前请求是否为 WordPress 的 cron 请求。它返回布尔值,并可通过 'wp_doing_cron' 过滤器进行自定义。

关键要点

  • 函数返回 true 表示当前是 WordPress cron 请求,否则返回 false。
  • 内部基于 DOING_CRON 常量定义进行判断,并应用 'wp_doing_cron' 过滤器。
  • 该函数自 WordPress 4.8.0 版本引入,常用于插件或主题开发中控制 cron 相关逻辑。

代码示例

if ( wp_doing_cron() ) {
    // 执行 cron 任务相关代码
} else {
    // 执行非 cron 请求的代码
}

📄 原文内容

Determines whether the current request is a WordPress cron request.

Return

bool True if it’s a WordPress cron request, false otherwise.

Source

function wp_doing_cron() {
	/**
	 * Filters whether the current request is a WordPress cron request.
	 *
	 * @since 4.8.0
	 *
	 * @param bool $wp_doing_cron Whether the current request is a WordPress cron request.
	 */
	return apply_filters( 'wp_doing_cron', defined( 'DOING_CRON' ) && DOING_CRON );
}

Hooks

apply_filters( ‘wp_doing_cron’, bool $wp_doing_cron )

Filters whether the current request is a WordPress cron request.

Changelog

Version Description
4.8.0 Introduced.