Schedule Additional Cron Job with cron_schedules WordPress hook

For Cron job, WordPress have three schedule time hourly, twicedaily and daily to add your event. All of them run on defined interval. Hourly runs in every hour, twicedaily runs in each 12 hours and daily runs in every 24 hours. To add a new schedule time, cron_schedules() filter is used. Below code will add three new cron schedule ( in_per_minute, in_per_ten_minute, three_hourly ).

Add Custom Schedules

add_filter( 'cron_schedules', 'w4dev_corn_schedules');
function w4dev_corn_schedules()
{
	return array(
		'in_per_minute' => array(
			'interval' => 60,
			'display' => 'In every Mintue'
		),
		'in_per_ten_minute' => array(
			'interval' => 60 * 10,
			'display' => 'In every two Mintues'
		),
		'three_hourly' => array(
			'interval' => 60 * 60 * 3,
			'display' => 'Once in Three minute'
		)
	);
}

Reference: interval is measured in seconds. 2 minute = 60 * 2 sec. display is the Display label name for this schedule.


Using the new schedule

First you will need to register an event for the schedule. You can register multiple events for one schedule. Upon registering an event with an schedule, a new action hook becomes available.

Schedule an Event

if( !wp_next_scheduled('w4dev_one_minute_event') )
{
	wp_schedule_event( time(), 'in_per_minute', 'w4dev_one_minute_event' );
}

This code will schedule a new event w4dev_one_minute_event to be run in every minute. That means, in each minute, an action hook – my_one_minute_event will be available once to call.


Call a function on scheduled event

You can hook to an event with your function. Even you hook multiple time using multiple function.

function w4dev_one_minute_job_cron()
{
    // your code here
}
add_action( 'w4dev_one_minute_event', 'w4dev_one_minute_job_cron' );

That’s it. All of the above code could be placed on your themes functions.php file or in plugin.