I am trying to debug a cron job in wordpress. It's supposed to update user hours but doesn't, and after putting some test statements in the code, I'm not too sure it's even running.
I have debug mode on, as in a put this in wp-config.php
:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', true);
And I call my cron event in functions.php
like so:
add_action('wp', 'my_activation');
add_action('update_user_hours', 'do_this_hourly');
function my_activation() {
if ( !wp_next_scheduled( 'update_user_hours' ) ) {
wp_schedule_event(time(), 'hourly', 'update_user_hours');
}
}
function update_user_hours(){
error_log('the function actually gets called')
}
Basically I cannot get any error_log
statements to print in my log file, and I'm not totally sure how to debug otherwise. There are no other errors showing up in the log file either.
For testing, I'm running the event with WP-Crontrol.
Any ideas on how to debug this?
Thanks!
UPDATE: Someone pointed out a mistake with adding the cron event and I have updated my code like so:
add_action('wp', 'my_activation');
add_action('update_user_hours', 'do_this_hourly');
function do_this_hourly(){
error_log('foo');
//update the user hours
}
function my_activation() {
if ( !wp_next_scheduled( 'update_user_hours' ) ) {
wp_schedule_event(time(), 'hourly', 'update_user_hours');
}
}
I also began to use wp-cli and was able to run the event successfully from there. I checked the php error log and there was nothing about my cron job in there.
The user hours are still not being updated. At this point it's possible it's an error with the script itself, but if it's crashing it isn't going to the php error log somehow. The error log I'm viewing is in /var/log/apache2
, I'm using a LAMP stack on Ubuntu 16. Any input on how to debug this?
Thanks again!
UPDATE (AGAIN): I found a syntax error and I'm now able to run the script with the wp-cli. It does exactly what I want. However, it doesn't seem to be running on its own every hour when I put it on the server. How can I make sure the event actually gets scheduled now that the script is working?
update_user_hours
function, it's likely it doesn't run because it shows a syntax error in the PHP error log. Have you contacted the WP Crontrol support? Have you tried running it via normal cron by using WP CLI? How are you getting the302
response?/var/log/apache2
is very likely an Apache log, not a PHP log, are you sure thaterror_log
actually works and that it's not being silenced? Have you tested a known good cron example? If you can run the code explicitly via WP CLI and still not get a message, then something else is wrong, and a major assumption somewhere is broken ( most probably theerror_log
assumption )wp
hook rather than theinit
hook?wp
hook. I just tried changing it to init but got the same result