Wordpress get_option in an external PHP file

I created a plugin that programmatically adds products to WooCommerce. The plugin works fine, but now I need to do a cron job that runs every 5 minutes to update the inventory.

I have a script all written, but I need to include the get_option () calls in this php file to get the specific plugin values ​​that the user entered. However, I can’t just include it get_option()in this file because it is outside the core of Wordpress. So I thought that I should put in require( 'path/to/wp-load.php' );, which, as I know, you really should not do. In any case, this fixes the problem if you click the page through a web browser request. However, the cron job does not work the moment this file is included, because somewhere with wp-load.php it sends HTTP_Header requests.

Any thoughts or solutions? I tried adding define('WP_USE_THEMES', false);wp-load.php directly above the request, but it still causes the cron job to fail.

For a long time I know, but how do you include queries get_option()inside an external PHP script that will be accessed through the cron PHP job.

Many thanks.

+4
source share
1 answer

Quick and easy way

Probably the problem is that you are trying to enable wp-load.php from the wrong path. In the CLI environment, the path will not be the same as when you make an HTTP request to the file. Thus, you should fix your problem:

require(dirname(__FILE__) . '/../../../wp-config.php');

Right but Longer Way

Based on the comments of cale_b and in this article that he linked, there is a very correct way to do this Wordpress Cron task .

, , , my_cron_job(). script, . , 5 :

// 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 :

*/5 * * * * php-cli -f [path to your WP]/wp-cron.php

Update

, wp-cron.php cron, WP Cron ( cron -):

define('DISABLE_WP_CRON', true);

-, WP Cron, . 100%, , , wp_schedule_event cron, , . script, cron.

:

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 - , 4min.

add_filter('cron_schedules', 'fourly_interval');
function fourly_interval($interval) {
    $interval['fourly'] = array('interval' => 4*60, 'display' => 'Once 4 minutes');
    return $interval;
}

, :

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 .

+1

Source: https://habr.com/ru/post/1620148/


All Articles