Make WordPress Core

Opened 7 years ago

Last modified 17 months ago

#25266 reviewing enhancement

wp_unschedule_event should apply filter to changes

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


wp_unschedule_event needs to announce removal of events, just like wp_schedule_event does through a filter

I would suggest allowing the filter to cancel the unschedule as a bonus.


function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
	$crons = _get_cron_array();
	$key = md5(serialize($args));
	$event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'args' => $args );	
	if ( apply_filters('unschedule_event', $event) ) 
		unset( $crons[$timestamp][$hook][$key] );
	if ( empty($crons[$timestamp][$hook]) )
		unset( $crons[$timestamp][$hook] );
	if ( empty($crons[$timestamp]) )
		unset( $crons[$timestamp] );
	_set_cron_array( $crons );

Optionally, consider if the filter returns false and there is no unset, the database should not be rewritten.

Attachments (1)

25266.patch (1.2 KB) - added by jrtashjian 5 years ago.

Download all attachments as: .zip

Change History (7)

#1 @mordauk
7 years ago

It makes sense that both wp_schedule_event() and wp_unschedule_event() have consistent filters.

#2 @sphoid
6 years ago

I would really like to see this happen. It is not possible to replace the cron system through a plugin without consistent filters/actions across all of the event functions.

#3 @chriscct7
5 years ago

  • Keywords needs-patch added
  • Severity changed from major to normal

5 years ago

#4 @jrtashjian
5 years ago

  • Keywords has-patch added; needs-patch removed

Patch adds a filter hook so we can hook into wp_unschedule_event before retrieving, removing, and updating the scheduled hooks. I left out the check to determine if we should write to the database because it's a cheap operation and probably not absolutely necessary. If people feel it should be there I'll add it in the future.

#5 @chriscct7
5 years ago

  • Owner set to chriscct7
  • Status changed from new to reviewing

#7 @peterwilsoncc
21 months ago

This is now possible with the changes introduced in WordPress 5.1, please refer to the dev note on the Make WordPress Core P2.

Note: See TracTickets for help on using tickets.