WordPress.org

Make WordPress Core

Opened 8 months ago

Last modified 8 months ago

#52448 new defect (bug)

wp_scheduled_delete WP-Cron Job Cannot Be Unscheduled

Reported by: daleharrison Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9
Component: Administration Keywords: 2nd-opinion
Focuses: Cc:

Description

My organization has disabled automatic trash deletion to preserve history on a large multisite network we host. We have done this via the following method:

<?php
remove_action( 'wp_scheduled_delete', 'wp_scheduled_delete' );

What we've noticed, however, is that WordPress still schedules the WP-Cron job of the same name (wp_scheduled_delete), and there does not appear to be a mechanism for disabling it.

wp_unschedule_event() is not capable of unscheduling the event because WordPress core always tries to schedule it if it's not already scheduled in wp-admin/admin.php:

<?php
// Schedule Trash collection.
if ( ! wp_next_scheduled( 'wp_scheduled_delete' ) && ! wp_installing() ) {
        wp_schedule_event( time(), 'daily', 'wp_scheduled_delete' );
}

The WP-Crontrol plugin shows a persistent warning about this, which is how we discovered it. See attached screenshot.

We are currently using WordPress 4.9.16, but it appears that the code noted above is the same in version 5.6.

What I would like to see is a better filter for this purpose that both removes the action and prevents the WP-Cron job from being (re)scheduled.

Attachments (1)

wp-crontrol-wp_scheduled_delete.png (33.0 KB) - added by daleharrison 8 months ago.
WP-Crontrol screenshot showing warning about wp_scheduled_delete.

Download all attachments as: .zip

Change History (4)

@daleharrison
8 months ago

WP-Crontrol screenshot showing warning about wp_scheduled_delete.

#1 @peterwilsoncc
8 months ago

  • Keywords 2nd-opinion added

Thanks for the report.

For now, I would recommend remove_action( 'wp_scheduled_delete', 'wp_scheduled_delete' ); after the default hooks have been added (probably wait until init, 5). Otherwise you could use pre_schedule_event or schedule_event hook to prevent the cron job from been scheduled.

I'm inclined to leave the above options as the best approach for this as never emptying the trash is an unusual use case and it's possible to do already in code.. I'll keep this open for now and get a second opinion.

#2 @johnbillion
8 months ago

Core _could_ check for the existence of an action on the wp_scheduled_delete hook before scheduling the event, we'd need to confirm that the default action is always added before the event is scheduled, and that plugins can remove the action before the event is scheduled.

The reason WP Crontrol warns about events with no action is because it helps guard against typos, actions that have unexpectedly gone missing, etc. It's not really a problem to have a cron event with no action, it's just useless.

Last edited 8 months ago by johnbillion (previous) (diff)

#3 @daleharrison
8 months ago

Thanks @peterwilsoncc for the recommendations.

I didn't have any luck with pre_schedule_event, but for my purposes, hooking into schedule_event and returning false for the event hook wp_scheduled_delete, alongside my existing wp_unschedule_event code has solved the problem.

This approach both removes the existing scheduled event and prevents it from being scheduled again.

Note: See TracTickets for help on using tickets.