Here’s the minified reference I use for setting up WordPress cron, which is all from wp_schedule_event()
and cron_schedules
:
Setup Cron
// SETUP CRON
add_action('wp', 'myplugin_schedule_cron');
function myplugin_schedule_cron() {
if ( !wp_next_scheduled( 'myplugin_cron' ) )
wp_schedule_event(time(), 'daily', 'myplugin_cron');
}
Cron Function
// the CRON hook for firing function
add_action('myplugin_cron', 'myplugin_cron_function');
#add_action('wp_head', 'myplugin_cron_function'); //test on page load
// the actual function
function myplugin_cron_function() {
// see if fires via email notification
wp_mail('[email protected]','Cron Worked', date('r'));
}
Custom Cron Time Interval
if WordPress’s default hourly, daily, aren’t enough, you can create your own.
// CUSTOM TIME INTERVAL
add_filter('cron_schedules', 'myplugin_cron_add_intervals');
function myplugin_cron_add_intervals( $schedules ) {
$schedules['customTime'] = array(
'interval' => 30,
'display' => __('Every 30sec')
);
return $schedules;
}
replacing daily
in wp_schedule_event()
with customTime
Viewing Crons
You can also view your cron via a gui with a plugin: https://wordpress.org/plugins/search/cron+view/