Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

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()

share|improve this answer
    
you are right! I should add it on register_activation_hook() –  ariefbayu Jan 26 '11 at 4:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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