Setting Up cron within wordpress


//******************************************************************************
//******************************************************************************
//********** REGISTER / DE-REGISTER OUR CRON FUNCTIONS WITH WORDPRESS **********
//******************************************************************************
//******************************************************************************
function my_cron_events_activate() {
    wp_schedule_event( time(), '5min', 'my_cron_main_event' );
}

function my_cron_events_deactivate() {
    wp_clear_scheduled_hook('my_cron_main_event');
}

//*************************************************
//***** CREATE OUR OWN CUSTOM SCHEDULE VALUES *****
//*************************************************
add_filter('cron_schedules','my_cron_schedules');
function my_cron_schedules($schedules)
{
  //Add our custom schedule times here (1 or more)
  if(!isset($schedules["5min"]))
  {
      $schedules["5min"] = array(
          'interval' => 5*60,
          'display' => __('Once every 5 minutes'));
  }
  
  /*
  if(!isset($schedules["30min"]))
  {
      $schedules["30min"] = array(
          'interval' => 30*60,
          'display' => __('Once every 30 minutes'));
  }
  */
  
  return $schedules;
}

Configuring WordPress to use your setup

Run this once to setup with the new configuration

  //Clear any old events out
  my_cron_events_deactivate();
  
  //Recreate all events
  my_cron_events_activate();

Add a cron schedule task to your server to call the wp cron

//    */5 * * * * wget -q -O - https://mydomainame.uk/wp-cron.php?doing_wp_cron

Your cron function

//***********************************
//***********************************
//********** CRON FUNCTION **********
//***********************************
//***********************************
add_action( 'my_cron_main_event',  'my_cron_main_event' );
function my_cron_main_event()
{


}
UK Electronic Designer
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.