Setting Up cron within wordpress

Standard recurrence values are ‘hourly’, ‘daily’ and ‘twicedaily’, or you can create your own as shown below.


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

  /*
  if (is_wp_error($Result))
    echo "wp_schedule_event error: " . $Result->get_error_message();
  else if ($Result === True)
    echo "wp_schedule_event success";
  else
    echo "wp_schedule_event something went wrong";
  */
}

function my_cron_events_deactivate() {
    wp_clear_scheduled_hook('my_cron_main_event');your namesace if namespace being used
}

//*************************************************
//***** CREATE OUR OWN CUSTOM SCHEDULE VALUES *****
//*************************************************
add_filter('cron_schedules','my_cron_schedules');    //<<<Change my_cron_schedules to a unique name (can't be shared by plugins or themes).
//add_filter('cron_schedules','/MyNamespaceName/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();
Using with a plugin

Use the wp_schedule_event() with your plugin activation hook register_activation_hook().

Ensure your also use your plugins register_deactivation_hook() to clear the scheduled event as deactivating a plugin doesn’t do it.

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' );    //<<<Change my_cron_main_event to a unique action name (can't be shared by plugins or themes).
//add_action( 'my_cron_main_event',  '/MyNamespaceName/my_cron_main_event' );
function my_cron_main_event()
{


}

Testing / Debugging

Run just your cron fucntion from code

Your cron function is created as an action, so you can trigger it to run from your own code using

do_action( 'my_cron_main_event' );
Run all wp cron functions

You can do this from a browser using:
https://mydomainname.com/wp-cron.php?doing_wp_cron

Remember though that wp will only trigger your function at the time interval you set for it, regardless of how often you trigger it

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.