Opened 19 months ago
Last modified 13 months ago
#18997 reopened feature request
Adding wp_unschedule_hook function
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Cron | Version: | 3.3 |
| Severity: | normal | Keywords: | has-patch needs-unit-tests |
| Cc: |
Description
Unschedule all previously scheduled cron job for a hook.
Can be usefull for plugins when deactivating to clean up the cron queue
The $hook parameter is required, so that the events can be identified.
@param string $hook Action hook, the execution of which will be unscheduled.
Attachments (3)
Change History (22)
comment:1
SergeyBiryukov — 19 months ago
Isn't that what wp_clear_scheduled_hook() already does?
That clears by hook + args, It's common place to hook multiple "instances" of a job with varying parameters (ie. Post ID to process) to a single hook, there's no API to clear all events for that hook at present. I've seen this requested before, it might be on trac, or elsewhere, I'm not sure.
comment:3
SergeyBiryukov — 19 months ago
That makes sense, I had a feeling I must have missed something.
- Keywords dev-feedback removed
dd32 is right :
"there's no API to clear all events for a hook at present"
i coded it myself for a plugin but think it ought to be in the API
just giving a bump to this ticket to have it included in 3.4
thank you
comment:6
Viper007Bond — 17 months ago
Patch seems to be okay although it will need to be cleaned up to meet WordPress coding standards.
comment:8
in reply to:
↑ 7
Viper007Bond — 17 months ago
Replying to arena:
Done !
Just one more minor nitpick:
if ( empty( $crons[$timestamp] ) ) unset( $crons[$timestamp] );
We do if's like that (line break in there) and with some extra spaces inside of the empty(). :)
- Milestone changed from Awaiting Review to 3.4
comment:11
nacin — 17 months ago
Perhaps too clever, or slower, but instead of if ( empty ) unset();, we could also hold off until before we call _set_cron_array() and then do $cron = array_filter( $cron ); That will clear out empty values.
comment:12
follow-up:
↓ 13
arena — 17 months ago
Nacin, your point is interesting.
May be this should be done once for all in
_set_cron_array()
simplifying all other functions calling _set_cron_array()
comment:13
in reply to:
↑ 12
arena — 17 months ago
Replying to arena:
Nacin, your point is interesting.
May be this should be done once for all in
_set_cron_array()
simplifying all other functions calling _set_cron_array()
since nacin tip, i reviewed the cron api and made some code changes :
- to have _get_cron_array closer to _set_cron_array when possible
- generalization of test empty( $crons ) after _get_cron_array()
- added a '$action' argument in function _set_cron_array()
patch is named #cron
comment:14
nacin — 14 months ago
- Milestone changed from 3.4 to Future Release
A good idea, but too late for 3.4.
comment:15
follow-up:
↓ 16
arena — 14 months ago
not fair, nacin opened 5 months ago ...
comment:16
in reply to:
↑ 15
nacin — 14 months ago
Replying to arena:
not fair, nacin opened 5 months ago ...
Cron had a few changes already in 3.4, specifically microtime locking. This however digs a bit deeper into the API and deserves a more thorough review. If we end up setting or getting the cron array out of order, we risk a bug or race condition. I think the patch will be great for 3.5.
comment:17
nacin — 14 months ago
- Keywords needs-unit-tests added
I am fine with reviving #18997.patch for 3.4, but we will first need unit tests: http://unit-tests.trac.wordpress.org/browser/wp-testcase/test_cron.php.
comment:18
arena — 13 months ago
- Resolution set to maybelater
- Status changed from new to closed
comment:19
Viper007Bond — 13 months ago
- Resolution maybelater deleted
- Status changed from closed to reopened
Please don't close valid tickets. Even if you no longer desire the feature, I'm sure others do. Thanks. :)

Isn't that what wp_clear_scheduled_hook() already does?