$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>debug - Wordpress Cron job, 302 response|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

debug - Wordpress Cron job, 302 response

matteradmin8PV0评论

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?

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?

Share Improve this question edited Nov 8, 2018 at 22:46 ellen asked Nov 7, 2018 at 18:14 ellenellen 3451 gold badge5 silver badges16 bronze badges 7
  • Note that there is a missing semicolon in your 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 the 302 response? – Tom J Nowell Commented Nov 7, 2018 at 20:39
  • Thanks for your reply! I added the error_log statement when writing my question after deleting the original contents of the function so the missing semicolon wasn't an issue turns out – ellen Commented Nov 8, 2018 at 6:56
  • /var/log/apache2 is very likely an Apache log, not a PHP log, are you sure that error_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 the error_log assumption ) – Tom J Nowell Commented Nov 8, 2018 at 16:23
  • Additionally, why is this cron job being managed on the wp hook rather than the init hook? – Tom J Nowell Commented Nov 8, 2018 at 16:25
  • I have never done this before and the example I saw used the wp hook. I just tried changing it to init but got the same result – ellen Commented Nov 8, 2018 at 18:01
 |  Show 2 more comments

1 Answer 1

Reset to default 1

The problem here is a mistake regarding how to schedule a cron event, lets begin with:

wp_schedule_event(time(), 'hourly', 'my_schedule_hook', $args);

wp_schedule_event(time(), 'hourly', 'update_user_hours');

Here you are telling WordPress to fire the update_user_hours action/event on an hourly basis.

You then hook into this to fire a callback:

add_action('update_user_hours', 'do_this_hourly');

But then, instead of declaring do_this_hourly, for some reason the code declares a mystery function named update_user_hours:

function update_user_hours(){
     error_log('the function actually gets called')
}

So the do_this_hourly function is undefined, a completely random new function is added that's totally disconnected, and to top it off, there's a missing semi colon. I would expect both syntax errors and missing callback warnings in your PHP error log

Extra Notes

  • WP_DEBUG_LOG doesn't enable the debug log, it just moves the PHP error log, you'd be better off using the real error log instead
  • Use WP CLI instead to trigger list and debug cron events, rather than a 3rd party plugin. Who knows what that plugin is doing or how it's meddling
  • Don't hide warnings, fix them. If you'd checked it would have lead you straight to the cause and the fix without having to come here
  • Try the Query Monitor plugin out, it'll flag warnings, errors and other things

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far