Setting Up cron within wordpress
Using standard recurrence values
Standard recurrence values are ‘hourly’, ‘daily’ and ‘twicedaily’.
//******************************************************************************
//******************************************************************************
//********** REGISTER / DE-REGISTER OUR CRON FUNCTIONS WITH WORDPRESS **********
//******************************************************************************
//******************************************************************************
//***** ACTIVATE *****
function my_cron_events_activate() {
//Deactive first to ensure there won't be repeated events
my_cron_events_deactivate();
$Result = wp_schedule_event( time(), 'hourly', 'my_cron_main_event' ); //<<<Namespace only needed if outside of the current namespace
/*
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";
*/
}
//***** DEACTIVATE *****
function my_cron_events_deactivate() {
wp_clear_scheduled_hook('my_cron_main_event');
//<<<Namespace only needed if outside of the current namespace
}
Using your own recurrence values
If you want something different from the standard recurrence values you can add the below to define them:
//Using in your activate function:
$Result = wp_schedule_event( time(), '5min', 'my_cron_main_event' );
//*************************************************
//***** 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
//Recreate all events
my_cron_events_activate();
//Delete all events
my_cron_events_deactivate();
Using with a plugin
Use the wp_schedule_event() with your plugin activation hook register_activation_hook().
Ensure you 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