wp_schedule_event not working?

I cannot get a WordPress cron to trigger. I am trying to run this code to see if I can get an email sent to me hourly.

It’s taken directly from the WordPress examples here: http://codex.wordpress.org/Function_Reference/wp_schedule_event. Except I removed underscores (see caveat in documentation).

The code is running in my theme functions.php file. The cron seems to be registered but the prefixdothishourly function does not get fired (as far as I can tell.)

add_action( 'wp', 'prefixsetupschedule' );

/**
 * On an early action hook, check if the hook is scheduled - if not, schedule it.
 */
function prefixsetupschedule() {
    if ( ! wp_next_scheduled( 'prefixhourlyevent' ) ) {
        wp_schedule_event(time(), 'hourly', 'prefixhourlyevent');
    }
}

add_action( 'prefixhourlyevent', 'prefixdothishourly' );

/**
 * On the scheduled action hook, run a function.
 */
 function prefixdothishourly() {
     return wp_mail("[email protected]", "Notification TEST", "TEST", null);
 }

2 Answers
2

Turns out I had to set the ALTERNATE_WP_CRON flag in wp-config.php to make this work:

define('ALTERNATE_WP_CRON', true);

Leave a Comment