Ok, let’s say I want to give the user the ability to schedule a wp cron event every X minutes. Now, let’s say I want X to be anywhere from 10 to 525600 (minutes in a year).
What I’m seeing is an utterly stupid way to handle cron events through the $schedules array. If I understand correctly, I have to add a custom schedule to the array for every single different event time the end user wants. For example:
add_filter( 'cron_schedules', function ( $schedules ) {
$schedules['twelve_minutes'] = array(
'interval' => 12,
'display' => __( 'Twelve Minutes' )
);
return $schedules;
} );
Please tell me there’s some way to schedule an event with a custom time without having to add 525,590 unique intervals to the $schedules array.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I use the following code to run a task. this doesn’t require adding a cron schedule.
function setup_my_action() {
if (!wp_next_scheduled('my_action')) {
wp_schedule_single_event(time()+3600, 'my_action');
}
}
function my_action() {
// do something
}
add_action('init','setup_my_action');
add_action('my_action', 'my_action');
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0