#52655 closed enhancement (fixed)
Ability to Change the wp_next_scheduled() function output.
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 6.8 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Cron API | Keywords: | has-patch |
Focuses: | Cc: |
Description
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 (10)
This ticket was mentioned in PR #1045 on WordPress/wordpress-develop by iamsayan.
4 years ago
#1
#3
@
4 years 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
@
4 years 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
@
4 years 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.
#7
@
2 months ago
- Milestone changed from Awaiting Review to 6.8
- Owner set to johnbillion
- Status changed from new to reviewing
#9
@
4 weeks ago
- Keywords add-to-field-guide added
@jeffpaul, as discussed in Slack I think it would be good to include this as a short note in the field guide:
Cron API: Introduce a new filter of the same name to
wp_next_scheduled()
. This allows plugin developers to modify the timestamp of the next scheduled event for a given wp-cron job. The full event object, hook name and arguments are provided as additional arguments for this filter.
Feel free to edit for length or clarity if you wish.
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