0

I created a wp cron event with a custom interval of 5 minutes, but it is only executed hourly instead of every 5 minutes. (The callback is properly executed.)

screenshot from wp crontrol plugin

The DISABLE_WP_CRON constant is set to true, wp-cron.php is called via crontab every 5 minutes. (https://developer.wordpress.org/plugins/cron/hooking-into-the-system-task-scheduler/)

No errors in debug.log (WP_DEBUG set to true).

I created my plugin with the wordpress plugin boilderplate generator (https://wppb.me/).

My code (in class Wp_Goldprice_Activator, function activate()):

    function fetch_metal_prices_recurrence( $schedules ) {
        $schedules['every_five_minutes'] = array(
            'display'  => 'every 5 minutes',
            'interval' => 300
        );
        return $schedules;
    }
    add_filter( 'cron_schedules', 'fetch_metal_prices_recurrence' );

    // Schedule custom-interval Cron Job Event
    if ( ! wp_next_scheduled ( GPR_PLUGIN_NAME . '_fetch_metal_prices' )) {
        wp_schedule_event( current_time( 'timestamp' ), 'every_five_minutes', GPR_PLUGIN_NAME . '_fetch_metal_prices' );
    }

    // Schedule hourly Cron Job Event
    if ( ! wp_next_scheduled ( GPR_PLUGIN_NAME . '_fetch_metal_prices_reschedule' )) {
        wp_schedule_event( current_time( 'timestamp' ), 'hourly', GPR_PLUGIN_NAME . '_fetch_metal_prices_reschedule' );
    }
}

I am rescheduling the 5 minute event every hour, because it disappears after first automatic execution (not on manual execution, though!). The hourly event doesn't disappear (on automatic execution). I couldn't find a reason or solution for that, either. This might be involved in the problem, but still, wp_next_scheduled() immediately returns a time which is 1 hour in the future, not 5.

I am aware, this question has been asked before, but no working solution has been proposed (and posted years ago):

Any thoughts on that would be greatly appreciated, I am out of ideas.

0

I know that this answer may not help you now because it's too late :) But here's a full example for a cronjob work in a plugin for the upcoming developers who will meet the same issue ;

////////////////////////////////////////////
/*         CRON Work STARTS HERE           */
/////////////////////////////////////////////
function sh_booking_cronstarter_activation() {
  if( !wp_next_scheduled( 'sh_booking_cronjob_hourly' ) ) {  
     wp_schedule_event( time(), 'hourly', 'sh_booking_cronjob_hourly' ,array());  
  }
  if( !wp_next_scheduled( 'sh_booking_cronjob_daily' ) ) {  
     wp_schedule_event( time(), 'twicedaily', 'sh_booking_cronjob_daily' ,array());  
  }
}
register_activation_hook (__FILE__, 'sh_booking_cronstarter_activation');

function sh_booking_cronstarter_deactivate() { 
  $timestamp = wp_next_scheduled ('sh_booking_cronjob_hourly');
  wp_unschedule_event ($timestamp, 'sh_booking_cronjob_hourly',array());

  $timestampp = wp_next_scheduled ('sh_booking_cronjob_daily');
  wp_unschedule_event ($timestampp, 'sh_booking_cronjob_daily',array());
} 
register_deactivation_hook (__FILE__, 'sh_booking_cronstarter_deactivate');

add_action ('sh_booking_cronjob_hourly', 'cron_sh_booking_cronjob_hourly_6cc9234b',10,0); 
add_action ('sh_booking_cronjob_daily', 'cron_sh_booking_cronjob_daily_6cc9234b',10,0); 

function cron_sh_booking_cronjob_hourly_6cc9234b(){
  /* your logic goes here ;) */
}
function cron_sh_booking_cronjob_daily_6cc9234b(){
  /* your logic goes here ;) */
}

Hop that helps ;)

  • thanks for the effort, but I don't see, how your solution will solve my problem of a 5min-cronjob only being executed hourly and it being deleted after one automatic execution? – jasie Jun 13 '18 at 6:37

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.