2

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 https://example.com/wp-cron.php. 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.)

2 Answers 2

2
+50

The WP_DISABLE_CRON constant only removes WP-Cron from loading on the page so it's no longer triggered by site traffic.

You can absolutely hit the wp-cron.php file directly to trigger it. I use https://cron-job.org to ping my private sites at https://dev.example.com/wp-cron.php?doing_wp_cron for example.

This is actually recommended in the WordPress handbook: https://developer.wordpress.org/plugins/cron/hooking-wp-cron-into-the-system-task-scheduler/

Editing to add: if you're concerned about triggering any other hooks associated with WP-Cron that maybe the site owner wants to avoid, you can also use cron-job.org (or a server CRON) to ping a page within your plugin to only run your update function without triggering WP-Cron at all.

1

When you add define('DISABLE_WP_CRON', true); in wp-config.php file the front end cron is disabled when website is triggered there will be no cron running at that time when you reload the page or visit the website.

So, if you want to run cron using system then you can use ( cron_schedules , wp_next_scheduled ) to run the cron at specific time.

Example :

add_filter( 'cron_schedules', 'check_every_time_cron' );
function check_every_time_cron( $schedules ) {
    $schedules['every_minutes'] = array(
        'interval'  => 60,
        'display'   => __( 'Every 1 Minutes', 'cera-child' )
    );
  return $schedules;
}
if ( ! wp_next_scheduled( 'check_every_time_cron' ) ) {
    wp_schedule_event( time(), 'every_minutes', 'check_every_time_cron' );
}
add_action( 'check_every_time_cron', 'check_every_time_cron_fun' );
function check_every_time_cron_fun() {
  try {
    custom_logs('djdjdjdjdj');
  }catch (\Exception $ex) {
    return apiResponse(200, true, $ex->getMessage());
  }
}

function custom_logs($message) {
  if(is_array($message)) {
    $message = json_encode($message);
  }
  $file = fopen("wp-content/themes/cera-child/custom_logs.log","a");
  echo fwrite($file, "\n" . date('Y-m-d h:i:s') . " :: " . $message);
  fclose($file);
}

It will check the cron and run at when you add ( wget -q -O - https://example.com/wp-cron.php?doing_wp_cron ) at specific time in crontab -e.

Example : 00 5 * * * wget -q -O - https://example.com/wp-cron.php?doing_wp_cron it is running every day at morning 5 am.

where i have caleed custom_logs() function you can add your function and do the work what you want.

I have used that in my created plugin with the use of REST API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.