Wordpress - How to debug WordPress "Cron" wp_schedule_event

You can run WP cron manually by calling: http://example.com/wp-cron.php?doing_wp_cron

If you don't want the automatic cron to run while you're debugging, then add this to your /wp-config.php file:

define('DISABLE_WP_CRON', true);

If you're on a development environment and want to output debug information, calling it manually like that will show you your debug output.

Alternatively you can use PHP's built-in error_log function to log message strings to the error log for debugging. You'd need to use this in conjunction with WP_DEBUG settings, as mentioned by Rarst.


You could use the plugin Cron-View. There you can see if your job is a) registered and b) what the next due time is.

In addition, you could add a lower schedule-timer to your event (e.g. every 2 min) and test your method more frequently on a local system. Use the 'cron_schedules' filter hook to register new schedule times. For example:

function my_additional_schedules($schedules) {
    // interval in seconds
    $schedules['every2min'] = array('interval' => 2*60, 'display' => 'Every two minutes');
    return $schedules;
}
add_filter('cron_schedules', 'my_additional_schedules');

You could debug manually, by creating an action and executing the Cron action inside. Like this:

add_action( 'init', function() {

    if ( ! isset( $_GET['the_cron_test'] ) ) {
        return;
    }

    error_reporting( 1 );

    do_action( 'this_is_cron_event_hook' );

    die();

} );

And by going to your website's address: http://example.com?the_cron_test

This should show you any errors with the cron task.

But it's without any sense doing it manually. You could use Advanced Cron Manager PRO plugin which does this for you and also saves the log and other stats.

Tags:

Cron