外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>WordPress开发>如何添加 WordPress 定时任务 WP-Cron

如何添加 WordPress 定时任务 WP-Cron

作为WordPress开发者,可能需要在程序中执行一些定时任务,今天我们就来分享下如何使用 WordPress cron 作业自动执行重复任务的方法。

关于 WP-Cron

WP-Cron 是 WordPress 中的内置工具,可让您安排自动化任务。

在《如何在 WordPress 中禁用 wp-cron 并设置正确的 Cron 定时任务》一文中,我们介绍了 WP-Cron 的弊端:WP-Cron 是根据网站页面加载触发的,因此取决于站点流量,流量过低导致任务没按时执行,流量过高导致服务器资源占用过多。

如果该站点没有获得足够的流量,则该任务只会在下一个用户访问该站点时执行,即使它已经应该运行。例如,如果您希望在上午 7:00 执行特定任务,但此时没有发生页面加载,则该任务将仅在下次访问时执行。

由于 WP-Cron 在每次页面加载时都会检查 cron 作业,这可能会导致性能下降,尤其是在高流量网站上。

我们建议使用服务器的计划任务替换 WordPress 自身 wp-cron 定时任务。这里说的替换,其实只是替换了触发操作,WordPress 的定时任务内容仍旧需要,而且可以正常工作。

我们可以使用内置的 WP-Cron 解决方案设置 cron 作业,但仅通过服务器端 cron 作业系统而不是在每个页面加载时主动运行文件/wp-cron.php。具体操作请看我们之前的文章《如何在 WordPress 中禁用 wp-cron 并设置正确的 Cron 定时任务

下面我们来将如何正确添加 WordPress cron 定时任务。

cron 任务的一般结构

/**
 * Schedules the cron job.
 */
add_action(
	'init',
	function() {
        // Create the event which will execute the automated task.
		add_action(
			'notesontech_refresh_feed_event',
			'notesontech_refresh_feed_run'
		);

        // Schedule the event.
		if ( ! wp_next_scheduled( 'notesontech_refresh_feed_event' ) ) {
			wp_schedule_event(
				strtotime( '07:00:00' ),
				'daily',
				'notesontech_refresh_feed_event'
			);
		}
	}
);

/**
 * Executes the cron job.
 */
function notesontech_refresh_feed_run() {
    // This is the actual task the we want to automate.
}

这里有两个主要部分。

第一部分是 WordPress 完成加载后触发的操作 ( init)。

在其中,我们为事件创建一个动作。这甚至会在计划任务时触发。然后它将触发实际任务。

如果还没有安排,我们还会安排下一次执行此任务的时间(使用wp_schedule_event)。

第二部分是实际任务——cron 作业。

在上面的示例中,该notesontech_refresh_feed_run函数将在每天daily格林威治标准时间上午 7:00 执行(注意第二个重复参数中的值)(注意strtotime( '07:00:00' )第一个时间戳参数中的值)。

时间戳

这是wp_schedule_event函数中的第一个参数。它定义了下一次运行事件的时间。

我们可以设置任务应该运行的特定时间strtotime( '07:00:00' )(更改07:00:00为所需时间)。

或者,如果我们只想让自动化任务运行并且具体时间并不重要,我们可以将time()其用作第一个参数来立即运行下一个事件。

在这种情况下,该任务将在部署此新代码后首次执行。然后,它会根据设置的循环重复自己。

重复执行

函数中的第二个参数wp_schedule_event。它定义了应该多久执行一次事件。

支持的默认值为hourlytwicedailydailyweekly

自定义重复时间

如果它不是默认值的一部分,我们还可以创建另一个重复选项。

假设我们要创建一个“每小时两次”重复来每 30 分钟运行一次任务:

/**
 * Adds custom cron schedules.
 *
 * @param array $schedules Existing schedules.
 * @return array Array of default and custom schedules.
 */
function notesontech_add_custom_cron_schedules( $schedules ) {

	// Every 30 minutes.
	$schedules['twicehourly'] = array(
		'interval' => HOUR_IN_SECONDS / 2,
		'display'  => __( 'Twice Hourly', 'textdomain' ),
	);

	return $schedules;
}

add_filter( 'cron_schedules', 'notesontech_add_custom_cron_schedules' );

具体日期

最后,假设我们想在特定日期执行一项任务。

例如,每个月的 15 日。

实现此目的的一种简单方法是设置具有daily重复性的标准任务。

然后,在实际任务中,首先检查日期。

如果日期不是 15,我们停止执行 ( return)。

这样,每天都会触发自动化任务,但实际上只在每个月的 15 日运行。

在以下示例中,cron 作业将在每月 15 日格林威治标准时间上午 7:00 执行。

/**
 * Schedules the cron job.
 */
add_action(
	'init',
	function() {
        // Create the event which will execute the automated task.
		add_action(
			'notesontech_refresh_feed_event',
			'notesontech_refresh_feed_run'
		);

        // Schedule the event.
		if ( ! wp_next_scheduled( 'notesontech_refresh_feed_event' ) ) {
			wp_schedule_event(
				strtotime( '07:00:00' ),
				'daily',
				'notesontech_refresh_feed_event'
			);
		}
	}
);

/**
 * Executes the cron job.
 */
function notesontech_refresh_feed_run() {

	// Run the cron job every day ("daily" interval) and check the date.
	// Only execute the cron job on the 15th of each month.
	if ( 15 !== (int) gmdate( 'd' ) ) {
		return;
	}

    // Continue with the automated task.
}

参考资料

拓展阅读:

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

更改 WordPress 自定义文章类型存档页面的文章数

2022-5-19 22:33:39

WordPress开发

如何为您的 WordPress 导航菜单添加样式

2022-8-11 10:30:49

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索