Make WordPress Core

Opened 7 weeks ago

Last modified 7 weeks ago

#52655 new enhancement

Ability to Change the wp_next_scheduled() function output.

Reported by: infosatech Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Cron API Keywords: has-patch
Focuses: Cc:


If I want to replace WP Cron with the Action Scheduler Library it is necessary to change the wp_next_scheduled() function output by overriding it using a filter to use as_next_scheduled_action() function.

function wp_next_scheduled( $hook, $args = array() ) {
    $next_event = wp_get_scheduled_event( $hook, $args );
    if ( ! $next_event ) {
        return false;
    return apply_filters( 'wp_next_scheduled', $next_event->timestamp, $next_event, $hook, $args );

If we use 'pre_get_scheduled_event' filter, it will also override the default Cron functions. So that I think 'wp_next_scheduled' is necessary.

Change History (6)

This ticket was mentioned in PR #1045 on WordPress/wordpress-develop by iamsayan.

7 weeks ago

Added 'wp_next_scheduled' filter to add the ability to change the wp_next_scheduled() function output.

Trac ticket: https://core.trac.wordpress.org/ticket/52655

#2 @johnbillion
7 weeks ago

  • Component changed from General to Cron API
  • Version 5.6.2 deleted

#3 @peterwilsoncc
7 weeks ago

@infosatech Hello, and welcome to trac!

If we use 'pre_get_scheduled_event' filter, it will also override the default Cron functions. So that I think 'wp_next_scheduled' is necessary.

Are you able to expand on this a little bit? I absolutely understand why you might wish to replace wp-cron with action scheduler but I am unclear as to why overriding the other wp-cron functionality is undesirable? Doesn't replacing cron require you to use all the pre_* filters within wp-cron?

I'm not opposed to adding the filter, by the way, it was something I considered back in 5.1 but ultimately decided against, see #45976.

#4 @infosatech
7 weeks ago

Hello @peterwilsoncc

I have written a plugin to automatically migrate WP Cron to the Action Scheduler library. Here is the link: https://github.com/iamsayan/migrate-cron-to-action-scheduler/

But Action Scheduler does not store recurrence value as WordPress stores using 'cron_schedules' filter. So, If I hijack wp_get_scheduled_event() function using filter 'pre_get_scheduled_event' then I can't set the recurrence. See here(https://github.com/iamsayan/migrate-cron-to-action-scheduler/blob/main/migrate-cron-to-action-scheduler.php#L299). I have to set it as false which is, I think, not recommended.

So I think just to hijack the wp_next_scheduled() function and not the wp_get_scheduled_event() function.

#5 @peterwilsoncc
7 weeks ago

Thanks for the further explanation @infosatech. There's a gotcha with not returning the named schedule (which I've logged on your GitHub repo).

@johnbillion has created a pull request for the new filter, I will review and follow up there.

#6 @peterwilsoncc
7 weeks ago

Sorry, I misread the email -- I now realise @infosatech has created the patch.

Note: See TracTickets for help on using tickets.