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

2022-08-04

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

如何添加 WordPress 定时任务 WP-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 任务的一般结构

  1. /**
  2. * Schedules the cron job.
  3. */
  4. add_action(
  5. 'init',
  6. function() {
  7. // Create the event which will execute the automated task.
  8. add_action(
  9. 'notesontech_refresh_feed_event',
  10. 'notesontech_refresh_feed_run'
  11. );
  12. // Schedule the event.
  13. if ( ! wp_next_scheduled( 'notesontech_refresh_feed_event' ) ) {
  14. wp_schedule_event(
  15. strtotime( '07:00:00' ),
  16. 'daily',
  17. 'notesontech_refresh_feed_event'
  18. );
  19. }
  20. }
  21. );
  22. /**
  23. * Executes the cron job.
  24. */
  25. function notesontech_refresh_feed_run() {
  26. // This is the actual task the we want to automate.
  27. }

这里有两个主要部分。

第一部分是 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 分钟运行一次任务:

  1. /**
  2. * Adds custom cron schedules.
  3. *
  4. * @param array $schedules Existing schedules.
  5. * @return array Array of default and custom schedules.
  6. */
  7. function notesontech_add_custom_cron_schedules( $schedules ) {
  8. // Every 30 minutes.
  9. $schedules['twicehourly'] = array(
  10. 'interval' => HOUR_IN_SECONDS / 2,
  11. 'display' => __( 'Twice Hourly', 'textdomain' ),
  12. );
  13. return $schedules;
  14. }
  15. add_filter( 'cron_schedules', 'notesontech_add_custom_cron_schedules' );

具体日期

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

例如,每个月的 15 日。

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

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

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

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

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

  1. /**
  2. * Schedules the cron job.
  3. */
  4. add_action(
  5. 'init',
  6. function() {
  7. // Create the event which will execute the automated task.
  8. add_action(
  9. 'notesontech_refresh_feed_event',
  10. 'notesontech_refresh_feed_run'
  11. );
  12. // Schedule the event.
  13. if ( ! wp_next_scheduled( 'notesontech_refresh_feed_event' ) ) {
  14. wp_schedule_event(
  15. strtotime( '07:00:00' ),
  16. 'daily',
  17. 'notesontech_refresh_feed_event'
  18. );
  19. }
  20. }
  21. );
  22. /**
  23. * Executes the cron job.
  24. */
  25. function notesontech_refresh_feed_run() {
  26. // Run the cron job every day ("daily" interval) and check the date.
  27. // Only execute the cron job on the 15th of each month.
  28. if ( 15 !== (int) gmdate( 'd' ) ) {
  29. return;
  30. }
  31. // Continue with the automated task.
  32. }
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明 1、本站所发布的全部内容源于互联网搬运,(包括源代码、软件、学习资料等)本站提供的一切软件、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的23个小时之内,从您的电脑或手机中彻底删除上述内容。
2、访问本站的用户必须明白,本站对所提供下载的软件和程序代码不拥有任何权利,其版权归该软件和程序代码的合法拥有者所有,如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。我们非常重视版权问题,如本站不慎侵犯您的版权请联系我们,我们将及时处理,并撤下相关内容!敬请谅解! 侵删请致信E-mail:messi0808@qq.com
3、如下载的压缩包需要解压密码,若无特殊说明,那么文件的解压密码则为www.xmy7.com
4、如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

小蚂蚁资源网 cms教程 如何添加 WordPress 定时任务 WP-Cron https://www.xmy7.com/zh/cms/8748.html

相关文章