Wordpress get_option在外部PHP文件中 [英] Wordpress get_option in External PHP file
问题描述
我创建了一个插件,将程序化的产品添加到WooCommerce。插件是很好的,但现在我需要一个cron工作,每5分钟运行一次更新库存。
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.
任何想法或解决方案?我试图添加 define('WP_USE_THEMES',false);
右上方的需要wp-load.php,但它仍然导致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.
我知道,但是如何在外部PHP脚本中包含 get_option()
通过PHP cron作业访问。
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 quick and easy way
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');
适当但更长的方式
在cale_b评论和这篇文章他链接,有是通过执行 Wordpress Cron作业非常适当的方式。
The proper but longer way
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执行 wp-cron.php 每5分钟:
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行为(通过网络访问执行cron) :
Update
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.
这篇关于Wordpress get_option在外部PHP文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!