我创建了一个插件,可以以编程方式将产品添加到 WooCommerce.该插件运行良好,但现在我需要创建一个每 5 分钟运行一次的 cron 作业来更新库存.
I have created a plugin that adds products programatically to WooCommerce. The plugin is working great, but now I need to make a cron job that runs every 5 minutes to update the inventory.
我已经编写了所有脚本,但我需要在此 php 文件中包含对 get_option() 的调用以获取用户输入的某些插件值.但是,我不能只在这个文件中包含 get_option()
,因为它在 Wordpress 核心之外.所以我的想法是放入 require( 'path/to/wp-load.php' );
我知道你不应该这样做.无论如何,如果您通过网络浏览器请求点击页面,它会解决问题.然而,cron 作业在包含此文件的那一刻失败,因为在 wp-load.php 的某个地方它正在发送 HTTP_Header 请求.
I have the script all written but I need to include calls to get_option() in this php file to get certain plugin values that the user has entered. However, I can't just include get_option()
in this file because it is outside of the Wordpress core. So my thought would be to put in require( 'path/to/wp-load.php' );
which I know you aren't really supposed to do. Anyway it fixes the issue if you hit the page via a web browser request. However the cron job fails the moment that this file is included because somewhere with wp-load.php it is sending HTTP_Header requests.
有什么想法或解决方案吗?我试图在 wp-load.php 要求的正上方添加 define('WP_USE_THEMES', false);
但它仍然导致 cron 作业失败.
Any thoughts or solutions? I tried to add define('WP_USE_THEMES', false);
right above the requiring of wp-load.php but it is still causing the cron job to fail.
冗长的我知道,但是您如何将 get_option()
请求包含在将通过 PHP cron 作业访问的外部 PHP 脚本中.
Long winded I know, but how do you include get_option()
requests inside of a external PHP script that will be accessed via a PHP cron job.
非常感谢.
问题可能是您试图从错误的路径中包含 wp-load.php.在 CLI 环境中,路径与您对文件执行 HTTP 请求时的路径不同.因此,您应该解决这个问题:
The problem is probably that you try to include wp-load.php from a wrong path. In a CLI environment, the path would not be the same as when you do an HTTP request to the file. So with this you should fixed your issue:
require(dirname(__FILE__) . '/../../../wp-config.php');
基于 cal_b 评论和这篇文章他链接,有一个非常正确的方法来做一个 Wordpress Cron 工作.
Based on cale_b comments and this article he linked, there is a much proper way to go by doing a Wordpress Cron job.
首先在你的插件中添加一个包含需要执行的代码的函数,我们称之为my_cron_job()
.您最终可以只包含您已经在此函数中编写的脚本.然后添加以下内容以安排每 5 分钟执行一次:
First in your plugin add a function that will contain the code needed to be executed, let's call it my_cron_job()
. You can eventually just include the script you already wrote in this function. Then add the following to schedule the execution of this every 5min:
// Define a new interval (5 minutes)
add_filter('cron_schedules', 'fively_interval');
function fively_interval($interval) {
$interval['fively'] = array('interval' => 5*60, 'display' => 'Once 5 minutes');
return $interval;
}
// Register the hook on plugin activation
register_activation_hook(__FILE__, 'my_cron_job_activation');
add_action('my_cron_event', 'my_cron_job');
function my_cron_job_activation() {
wp_schedule_event(time(), 'fively', 'my_cron_event');
}
// Unregister the hook on plugin deactivation
register_deactivation_hook( __FILE__, 'my_cron_job_deactivation' );
function my_cron_job_deactivation(){
wp_clear_scheduled_hook( 'my_cron_event' );
}
然后将您的 cron 设置为每 5 分钟执行一次 wp-cron.php:
Then set up your cron to execute wp-cron.php every 5 minutes:
*/5 * * * * php-cli -f [path to your WP]/wp-cron.php
<小时>
首先使用服务器cron选择执行 wp-cron.php 的选项时,您应该禁用默认的WP CRON行为(通过Web访问的Cron执行):
First when choosing the option of executing wp-cron.php with a server cron you should disable the default WP Cron behaviour (execution of cron through web visits):
define('DISABLE_WP_CRON', true);
其次,至于您关于 WP Cron 可靠性的问题,我确实看到了一个潜在的缺陷.我不是 100% 确定,但我认为 wp_schedule_event
有可能与服务器 cron 不同步,因为只有在间隔过去时才会执行作业.因为它将根据脚本的执行时间重新安排,与服务器 cron 时间略有不同.
Secondly, as for your question about WP Cron reliability I see a potential flaw indeed. I'm not 100% sure of that, but I think it is possible that wp_schedule_event
get desynchronized with the server cron, as the job get executed only if the interval is past. As it will be re-scheduled depending of the execution time of the script which is slightly different with the server cron time.
例如:
00:00:00:000 Server cron execute wp-cron.php
00:00:00:100 The job can be executed, so let it run
00:00:00:200 Wordpress finished to execute the job - it schedule the event in 5min
00:05:00:000 Server cron execute wp-cron.php
00:05:00:100 The job is planned for 00:05:00:200, no execution !
00:10:00:000 Server cron execute wp-cron.php
00:10:00:100 The job is executed
那当然是理论,也许这不准确.我建议做一些测试,看看它的行为.如果它的行为确实像我认为的那样,我建议将 wp_schedule_event
更改为较低的间隔 - 例如 4 分钟.
That's theory of course, maybe this is not accurate. I suggest doing some test and see how it behave. If it indeed behave like I think it did, I suggest as easy workaround to change the wp_schedule_event
to a lower interval - 4min for example.
add_filter('cron_schedules', 'fourly_interval');
function fourly_interval($interval) {
$interval['fourly'] = array('interval' => 4*60, 'display' => 'Once 4 minutes');
return $interval;
}
所以我们将有以下内容:
So we'll have the following:
00:00:00:000 Server cron execute wp-cron.php
00:00:00:100 The job can be executed, so let it run
00:00:00:200 Wordpress finished to execute the job - it schedule the event in 4min
00:05:00:000 Server cron execute wp-cron.php
00:05:00:100 The job is planned for 00:04:00:200, so let it run!
00:10:00:000 Server cron execute wp-cron.php
00:00:00:200 Wordpress finished to execute the job - it schedule the event in 4min
00:10:00:100 The job is executed (planned for 00:09:00:200)
禁用默认的 WP Cron 行为后,它应该可以完美运行.
With the default WP Cron behaviour disabled it should work flawlessly.
这篇关于外部 PHP 文件中的 Wordpress get_option的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!