<tfoot id='cGyGr'></tfoot>

<i id='cGyGr'><tr id='cGyGr'><dt id='cGyGr'><q id='cGyGr'><span id='cGyGr'><b id='cGyGr'><form id='cGyGr'><ins id='cGyGr'></ins><ul id='cGyGr'></ul><sub id='cGyGr'></sub></form><legend id='cGyGr'></legend><bdo id='cGyGr'><pre id='cGyGr'><center id='cGyGr'></center></pre></bdo></b><th id='cGyGr'></th></span></q></dt></tr></i><div id='cGyGr'><tfoot id='cGyGr'></tfoot><dl id='cGyGr'><fieldset id='cGyGr'></fieldset></dl></div>

    1. <small id='cGyGr'></small><noframes id='cGyGr'>

      <legend id='cGyGr'><style id='cGyGr'><dir id='cGyGr'><q id='cGyGr'></q></dir></style></legend>
      • <bdo id='cGyGr'></bdo><ul id='cGyGr'></ul>

      外部 PHP 文件中的 Wordpress get_option

      Wordpress get_option in External PHP file(外部 PHP 文件中的 Wordpress get_option)
          <tbody id='y81cT'></tbody>
        • <bdo id='y81cT'></bdo><ul id='y81cT'></ul>

            <small id='y81cT'></small><noframes id='y81cT'>

          1. <i id='y81cT'><tr id='y81cT'><dt id='y81cT'><q id='y81cT'><span id='y81cT'><b id='y81cT'><form id='y81cT'><ins id='y81cT'></ins><ul id='y81cT'></ul><sub id='y81cT'></sub></form><legend id='y81cT'></legend><bdo id='y81cT'><pre id='y81cT'><center id='y81cT'></center></pre></bdo></b><th id='y81cT'></th></span></q></dt></tr></i><div id='y81cT'><tfoot id='y81cT'></tfoot><dl id='y81cT'><fieldset id='y81cT'></fieldset></dl></div>
              <legend id='y81cT'><style id='y81cT'><dir id='y81cT'><q id='y81cT'></q></dir></style></legend>

                <tfoot id='y81cT'></tfoot>
                本文介绍了外部 PHP 文件中的 Wordpress get_option的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我创建了一个插件,可以以编程方式将产品添加到 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 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');
                

                正确但更长的方法

                基于 cal_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 设置为每 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执行):


                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.

                这篇关于外部 PHP 文件中的 Wordpress get_option的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                How to parse django style template tags(如何解析 django 样式模板标签)
                What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                php in background exec() function(php 后台 exec() 函数)
                  <tbody id='BjMvL'></tbody>
              1. <small id='BjMvL'></small><noframes id='BjMvL'>

                • <tfoot id='BjMvL'></tfoot>
                  • <bdo id='BjMvL'></bdo><ul id='BjMvL'></ul>
                    <i id='BjMvL'><tr id='BjMvL'><dt id='BjMvL'><q id='BjMvL'><span id='BjMvL'><b id='BjMvL'><form id='BjMvL'><ins id='BjMvL'></ins><ul id='BjMvL'></ul><sub id='BjMvL'></sub></form><legend id='BjMvL'></legend><bdo id='BjMvL'><pre id='BjMvL'><center id='BjMvL'></center></pre></bdo></b><th id='BjMvL'></th></span></q></dt></tr></i><div id='BjMvL'><tfoot id='BjMvL'></tfoot><dl id='BjMvL'><fieldset id='BjMvL'></fieldset></dl></div>
                    1. <legend id='BjMvL'><style id='BjMvL'><dir id='BjMvL'><q id='BjMvL'></q></dir></style></legend>