0

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.

4
  • What's the scenario where you would want to run a cronjob at 12 minutes and another one 13 minutes? Why not just combine them both into one or the other? WordPress has default intervals which should work in most scenarios. developer.wordpress.org/plugins/cron/…
    – Howdy_McGee
    Feb 24, 2021 at 18:21
  • Imagine a scenario where the plugin is checking the status of other websites. Let's say the end user wants to check site A every 2 weeks. Site B every 10 minutes, Site C every 6 hours. That would be 3 more custom additions to the $schedules. Now let's imagine that same end user wants to monitor not 3, but 100 websites. The purpose of this mental experiment is to create a set of WP cron jobs where the user doesn't have to keep going into Linux to create them. They can create one cron job in the OS to fire up WP every 10 minutes. From there, WP cron handles the rest.
    – uPrompt
    Feb 24, 2021 at 18:57
  • WordPress Cronjobs are not your traditional Server Cronjobs. They will not run every X minutes exactly. The cronjobs run whenever someone visits the website, at which point WordPress will run all events that meet the time or are past-due. From the docs: "Note that scheduling an event to occur within 10 minutes of an existing event with the same action hook will be ignored unless you pass unique $args values for each scheduled event.". It's not perfect, there are limitations. Unfortunately, I don't think there's a way around making schedules without making your own wp_schedule_event function.
    – Howdy_McGee
    Feb 24, 2021 at 19:17
  • That's what I was seeing. Just wanted to make sure I wasn't being stoopid. Thanks.
    – uPrompt
    Feb 24, 2021 at 19:48

1 Answer 1

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');
1
  • For a one off task, that works. I'm looking at wp_schedule_event() without loading up the $schedule array with a gob of different schedules. Your solution would work but means that every time the action fired, it would have to schedule itself again. A bit cumbersome but a solution.
    – uPrompt
    Feb 25, 2021 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.