0

Reading wp_schedule_event() documentation got me code something like this:

add_action('cr_paid_link_manager_generate_expiring_link_email_action','cr_paid_link_manager_generate_expiring_link_email');
wp_schedule_event(time(), 'daily', 'cr_paid_link_manager_generate_expiring_link_email_action');

The problem is, cr_paid_link_manager_generate_expiring_link_email() is generated everytime page load, or so it look's like. This code supposed to send email only once a day.

I try to set first parameter of wp_schedule_event() to $timestamp_start:

$timestamp_start = '2011-01-26 00:00:00';
add_action('cr_paid_link_manager_generate_expiring_link_email_action','cr_paid_link_manager_generate_expiring_link_email');
wp_schedule_event($timestamp_start, 'daily', 'cr_paid_link_manager_generate_expiring_link_email_action');

Still no result. The code keep sending email Every minute. Is there something missing on my code?

3

Where are you putting the wp_schedule_event() code? My guess is, you have it somewhere that's causing it to run on every page load, causing the action to be scheduled multiple times (like maybe you just dropped it in your functions.php?).

You only need to schedule the action once. The easiest way is to do it on activation of your plugin. Try something like:

function wpsx_7780_myplugin_init() {

  wp_schedule_event(time(), 'daily', 'cr_paid_link_manager_generate_expiring_link_email_action');

}
register_activation_hook( __FILE__, 'wpsx_7780_myplugin_init' );
add_action('cr_paid_link_manager_generate_expiring_link_email_action','cr_paid_link_manager_generate_expiring_link_email');

Then deactivate and reactivate your plugin. This way, the event only gets scheduled once, but the action gets added on every page load.

EDIT: I should also note that you'll probably need to clear all those existing events you've created using wp_clear_scheduled_hook()

  • you are right! I should add it on register_activation_hook() – ariefbayu Jan 26 '11 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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