2

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('[email protected]', '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!

1 Answer 1

3

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?

12
  • Sorry, I'm having trouble understanding your first two points... cron arg usage? logs? Could you please elaborate/reword? I have tested the send_reminders function on its own, including wp_mail, so yes I do know that that works. Thanks for the response!
    – Gavin
    Oct 21, 2010 at 23:28
  • re the first question, by the time the cron gets fired, your variable is no longer set (because it's a different process) to whatever it had been used with. re the second, you can pass arguments to the wp_schedule_single_event() function; give it an insert_id argument, and pass it to your function. Contrary to what the WP codex says, the actual signature is: wp_schedule_single_event( $timestamp, $hook, $args = array()). (see wp-includes/cron.php) Oct 22, 2010 at 1:41
  • Ahhh, that's helpful - the documentation is very skewed with regards to passing arguments to the scheduled action. However, even when I remove the variable from the action and just use hard coded text in the email, it still never sends.. any ideas for what else I can out in that action to see if it works with something other than wp_mail, perhaps? I have also tried mail.
    – Gavin
    Oct 22, 2010 at 13:32
  • For logs, I personally append to a file... Oct 22, 2010 at 16:21
  • Sorry for the delay - I still can't get the function to run. I have the function itself successfully writing a 'completed' message, but it never happens when the cron task disappears from the cron monitor. I've tried putting the function in my plugin's file and in my theme's functions file.
    – Gavin
    Oct 25, 2010 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.