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' );
function my_cron_hourly_event() {

  
  //Do something

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

function my_cron_events_deactivate() {
    wp_clear_scheduled_hook('my_cron_hourly_event');
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.