I’m trying to set up individual, one-time schedule events in a plugin. No matter what I do, I can’t seem to get the events to fire. I’m using the Cron View plugin to see what’s in queue, and the events are added and remove themselves totally on schedule. I never, however, receive the email I’ve set to send in the action’s target function (simply in order to test the event, there will be more in there later). I have tested the function outside of scheduled events, it does work. Here’s some code:
GLOBAL $new_workshop_id;
$new_workshop_id = $wpdb->insert_id;
function send_reminders() {
GLOBAL $new_workshop_id;
wp_mail('my@emailaddress.tld', 'Automatic ID: '.$new_workshop_id, 'here it is!');
}
if (!wp_next_scheduled('send_reminder_emails_'.$new_workshop_id)) {
wp_schedule_single_event( time()+30, 'send_reminder_emails_'.$new_workshop_id );
}
do_action( 'send_reminder_emails_'.$new_workshop_id );
add_action( 'send_reminder_emails_'.$new_workshop_id, 'send_reminders' );
One thing I’m suspicious is the placement of do_acton and the function itself, send_reminders()
. The above code is within some if statements checking for POST vars, so it’s possible that the function can’t be accessed by the CRON job – so where should I put the function? I’ve tried right at the top of the plugin file. From what I’ve read, do_action should call the function and execute it wherever you have put do_action, but I guess I need to know where to put send_reminders()
in the first place so that it’s accessible, by either the cron job or do_action.
Thanks in advance for any input!
The cron argument usage looks weird. The insert id will return garbage by the time your function is called.
By the same token, I believe better schedule it always, with the id argument, on insert, if this is for logging. Else you’ll be throttling your logs, basically.
Lastly, are you sure that
wp_mail()
is working?