WordPress Cron – Synchronize to the exact current hour

Home Forums BulletProof Security Pro WordPress Cron – Synchronize to the exact current hour

Viewing 1 post (of 1 total)
  • Author
  • #15516
    AITpro Admin

    An incredibly simple way to get your WordPress Cron Job to synchronize to the exact hour when it is scheduled for the first time and also each time the WordPress Cron Job is rescheduled. The $clock variable syncs to the exact UNIX hour using the PHP mktime() function – ie 5:00:00, 6:00:00, 7:00:00. Let’s say it is 9:38 am right now and the init action is fired – normally the cron job would be scheduled for “now” 9:38 am, but using the $clock variable the scheduled Cron Job is for 9:00:00 am exactly. At 10:01 am the cron will be rescheduled again to the exact hour 10:00:00 am.

    add_action('example_check', 'example_processing');
    function example_cron( $schedules ) {
    $schedules['hourly'] = array( 
    	'interval' => 3600, 
    	'display' => __('Hourly') );
    	return $schedules;
    add_filter('cron_schedules', 'example_cron');
    function example_checks() {
    $example_check = wp_get_schedule('example_check');
    $clock = mktime( date( "H", time() ), 0, 0, date( "n", time() ), date( "j", time() ), date( "Y", time() ) );
    	if ( !wp_next_scheduled('example_check') ) {
    		wp_schedule_event( $clock, 'hourly', 'example_check' );
    add_action('init', 'example_checks');
    function example_processing() {
    // do something here when the cron fires and executes/processes this function
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.