plugin development – Running one-off cron jobs when WP_DISABLE_CRON is true: can I hit /wp-cron.php?


I have a plugin that needs to do a “database update” sort of operation, going through all users and performing a particular database operation on each one. It’s designed for sites with large numbers — tens of thousands — of users. I’ve worked out a good efficient way to do the operation in chunks of 1000 users. And I’ve worked out a way for each chunk (except the last one) to schedule the next chunk for a couple of seconds later with wp_schedule_single_event(). So the “database update” runs for a while as a polite background job.

All good.

Now I’m trying to make this work on sites that use system cron and have WP_DISABLE_CRON set to true. My question is this: Is there anything drastically wrong with doing this from an unload action? This code does what a system cronjob does to the site: it hits But it does so more often while my background job is in progress.

if ( ! wp_doing_cron() ) {
  $url = get_site_url( null, 'wp-cron.php' );
  $req = new \WP_Http();
  $req->get( $url );

I try to respect the disabled internal cron: the site owner disabled it for good reasons. I am taking care not to do this unless I know there’s a chunk to process and I know it’s been at least couple of seconds since the last time I did it.

This looks like it works. But are there configurations where it will cause big trouble?

(Note: wp-cron.php itself does fastcgi_finish_request() promptly when it is invoked, so my code won’t hang for long waiting for wp-cron.php to finish.)

O. Jones 3 weeks 2022-09-03T05:37:58-05:00 0 Answers 0 views 0

Leave an answer