In this short note, I’ll show a simple example of working with cron in a popular engine like WordPress.
For a regularly recurring task (i.e., code that should run on a regular basis), you can use the following code snippet:
add_filter('cron_schedules', function ( $schedules ) {
$schedules['10sec'] = array(
'interval' => 10,
'display' => __('Every 10 sec'),
);
return $schedules;
});
add_action('init', function(){
if(!wp_next_scheduled('post_event_cron_action'))
{
wp_schedule_event( time(), '10sec', 'post_event_cron_action');
}
});
add_action('post_event_cron_action', function () {
echo 'Mail sent';
mail('test@example.com', 'Test subject', 'Test body');
});