0

I have a specific task that needs to run once (and only once) per day. Because of the uncertainty of wp-cron running every day I'm using setcronjob.com to activate the script.

The URL setcronjob.com calls is:

http://mydomain.co.uk/wp-cron.php?doing_wp_cron

And Daily Exec in the setcronjob.com settings is set to 1.

In wp-config.php I have added the line:

define('DISABLE_WP_CRON', true);

The script itself is a plugin, the code (minus the plugin stuff) is below:

<?php
add_action('init','kl_create_single_schedule');
add_action('kl_single_cron_job','kl_single_cron_function');

function kl_single_cron_function(){
  $todayis = date("Y-m-d");
  $thenwas =  date('Y-m-d H:i:s',  strtotime($todayis . '-360 day'));
  global $wpdb;         
  $renewals = $wpdb->get_results("SELECT * FROM {$wpdb->users} WHERE date({$wpdb->users}.user_registered) = '" . $thenwas ."'");                
  foreach ( $renewals as $renewal ) {
    $thename = $renewal->display_name;
    $theemail = $renewal->user_email;
    $to      = $theemail;
    $subject = 'Important information about your Subscription';
    $message = 'Hi ' . $thename . ",\n\r\n\rIn five days your subscription will renew and another year's subscription (£45) will be taken from your PayPal account.\r\n\r\nIf you want to stay with us you don't need to do anything, it will happen automatically. If you want to cancel your subscription you can do this via your PayPal account, or if you prefer simply reply to this email or send a new email to us at info@mydomain.co.uk requesting for your subscription to be cancelled.  \r\n\r\nRegards,\r\n\r\Kev from Domain Name.";
    $headers = 'From: info@mydomain.co.uk' . "\r\n" . 'Reply-To: info@mydomain.co.uk' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $message, $headers);
  } 
}

function kl_create_single_schedule(){
  //check if event scheduled before
  if(!wp_next_scheduled('kl_single_cron_job'))
  //schedule event to run after 1 day
  wp_schedule_single_event (time()+0001, 'kl_single_cron_job');
}
?>

However, whats happening is that the plugin is running multiple times per day.

Am I correct in thinking that the line:

wp_schedule_single_event (time()+0001, 'kl_single_cron_job');

Should actually be:

wp_schedule_single_event (strtotime('+1 day'), 'kl_single_cron_job');
2

wp_schedule_single_event's $timestamp is the time you want the event to occur. This must be in a UNIX timestamp format. WP cron uses UTC/GMT time, not local time. Use time(), which is always GMT in WordPress.

time() number of seconds since the Unix Epoch

If you want to add a day, you need to add seconds:

time() + (24 * 60 * 60)

So you would want to use:

if ( ! wp_next_scheduled( 'kl_single_cron_job' ) ) {

    wp_schedule_single_event( time() + 86400, 'kl_single_cron_job' );

}

or 'daily' with wp_schedule_event:

if ( ! wp_next_scheduled( 'kl_single_cron_job' ) ) {

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

}

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.