You can disable the default triggering of wp-cron.php for any page load and instead have it triggered using the system cron. This is based on the nifty guide here:

https://tommcfarlin.com/wordpress-cron-jobs/

Disable defult wordpress cron

Stop wp-cron.php being called for every page load (this is actually not a bad thing to do for heavily used sites). In wp-config.php file add the following line:

/*Disable default wordpress cron call for every page load (we are setting the system cron to call it instead at specific times)*/
define('DISABLE_WP_CRON', true);
Set your servers cron to trigger the wordpress cron

Use your servers management software or the command line to run the following cron task:

*/15 * * * * wget -q -O - http://MYDOMAINNAME.com/wp-cron.php?doing_wp_cron
Create your function to be triggered

Create functions to register and de-register the cron with wordpress

If creating a plugin you can register when the plugin is installed and de-register if it is deleted. Otherwise create some way to carry out the register function call.

//***********************************
//***********************************
//********** CRON FUNCTION **********
//***********************************
//***********************************
add_action( 'my_cron_hourly_event',  'my_cron_hourly_event' );    //<<<Need to add namespace??
function my_cron_hourly_event() {

  
  //Do something

}
Set it to be triggered by cron
//******************************************************************************
//******************************************************************************
//********** REGISTER / DE-REGISTER OUR CRON FUNCTIONS WITH WORDPRESS **********
//******************************************************************************
//******************************************************************************
function my_cron_events_activate() {
    wp_schedule_event( time(), 'hourly', 'my_cron_hourly_event' );    //<<<Need to add namespace??
}

function my_cron_events_deactivate() {
    wp_clear_scheduled_hook('my_cron_hourly_event');    //<<<Need to add namespace??
}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *