作为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。它定义了应该多久执行一次事件。
支持的默认值为hourly、twicedaily、daily和weekly。
自定义重复时间
如果它不是默认值的一部分,我们还可以创建另一个重复选项。
假设我们要创建一个“每小时两次”重复来每 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.
}
本网站名称:创码者资源网
本站永久网址: http://www.cmzym.top/
本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
本站为非盈利网站,一切付费内容(包括但不限于会员充值,资源付费),均为用户自愿赞助,且所有资金均用于网站维护
本站资源均收录于互联网,所以不能保证每个细节都符合你的要求,也可能存在未知的BUG与瑕疵,因虚拟资源可复制特性,所以不接受任何理由的退款兑现,请阅 读本站声明和相关条款后再进行支付下载
本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长 :1326632303@qq.com 进行删除处理。
本本站采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可


















暂无评论内容