0

Setting:

I have a wordpress site but disabled wp_cron to have the full control of cron.

define('DISABLE_WP_CRON', true);

In crontab -e, I have following cron job:

*/2 * * * * /usr/bin/php /var/www/cron/mycron.php init >> /var/log/error.log 2>&1

The mycron.php has a simple function

if (!empty($argv[1])) {        
    switch ($argv[1]){
    case 'init':
        cron_test();
        break;
    }
}
function cron_test() {
    $time = date(DATE_RFC822, time());
    write_log("Start:" . $time);   //outputs debug to my own log file   
};

function write_log($log){
    if ( true === WP_DEBUG ) {
        if ( is_array( $log ) || is_object( $log ) ) {
            write_log( print_r( $log, true ) );
        } else {
            write_log( $log );
        }
    }
};

Note that I declared the mycron.php in functions.php for wp:

require_once('parts/mycron.php'); 

Error log:

In my error.log for the cron, I have the following error:

PHP Warning:  Use of undefined constant WP_DEBUG - assumed 'WP_DEBUG'

So, I am guessing there is some sort of disconnection between cron and wp, which is my best guess.

What I am trying to do:

The mycron.php will have many wordpress functions that I would need. How do I make the cron to recognize the wp function such as WP_DEBUG?

Any help will be much appreciated.

Thanks!

Steve Kim
  • 5,293
  • 16
  • 54
  • 99

1 Answers1

2

You need to load Wordpress functions manually, to use them in a custom script.

require_once("../../../../wp-load.php");

Also answered in depth here,

How to include Wordpress functions in custom .php file?

Karl Johan Vallner
  • 3,980
  • 4
  • 35
  • 46