This document discusses how to automate tasks in WordPress using WP-Cron. WP-Cron allows scheduling single events or recurring tasks similarly to the Unix Cron utility. It covers scheduling events, adding custom schedules, cancelling tasks, and looking up scheduled tasks. While WP-Cron works across operating systems, it only runs when a site is visited so alternative cron services may be necessary for high traffic sites.
2. What is WP-Cron?
!
A task scheduler built into WordPress.
!
Named after the Unix Cron utility:
!
“The software utility cron is a time-based job scheduler in Unix-like
computer operating systems. People who set up and maintain software
environments use cron to schedule jobs (commands or shell scripts) to run
periodically at fixed times, dates, or intervals. It typically automates system
maintenance or administration—though its general-purpose nature makes
it useful for things like connecting to the Internet and downloading email at
regular intervals.[1] The name cron comes from the Greek word for time,
χρόνος chronos.”
!
- Wikipedia
!
!
3. Scheduling a Single Event
!
The wp_schedule_single_event() function is used to schedule an event to run once.
This could be used, for example, to send a follow up e-mail to a user at a scheduled
time after they register on your site:
!
add_action(‘user_register’, ‘schedule_followup_email’, 10, 1);
!
function schedule_followup_email($user_id) {
$userId = get_current_user_id();
wp_schedule_event(time() + 3600, ‘event_sendFollowupEmail’, array($user_id));
}
!
function sendWelcomeEmail($user_id) {
$user =get_user_by(‘id’, $user_id);
wp_mail($user->user_email, ‘Follow Up’, ‘Hope you’ve been enjoying our site’);
}
add_action(‘event_sendFollowupEmail’, ‘sendFollowupEmail’);
!
!
4. Scheduling Recurring Tasks
!
WP-Cron can run tasks at regular intervals
!
In the core:
!
• Checking if WordPress version is the most current
• Checking for theme and plugin updates
!
Functions:
!
• wp_get_schedule()
• wp_schedule_event()
• wp_get_next_scheduled()
• wp_clear_scheduled_hook()
• wp_unschedule_event()
!
Filter:
!
• cron_schedules
5. Schedules
Out of the box, WordPress can run recurring tasks:
!
• Daily (once every 24 hours)
• Twice Daily (every 12 hours)
• Hourly (every 60 minutes)
!
You can lookup supported schedules using wp_get_schedules()
!
array (size=3)
'hourly' =>
array (size=2)
'interval' => int 3600
'display' => string 'Once Hourly' (length=11)
'twicedaily' =>
array (size=2)
'interval' => int 43200
'display' => string 'Twice Daily' (length=11)
'daily' =>
array (size=2)
'interval' => int 86400
'display' => string 'Once Daily' (length=10)
6. Need Another Schedule?
You can create new schedules using the cron_schedules filter.
!
function add_new_cron_intervals($schedules) {
!
$schedules[‘weekly’] = array(
‘interval’ => 604800,
‘display’ => __(‘Once a week’)
);
!
$schedules[‘fourhours’] = array(
‘interval’ => 60 * 60 * 4,
‘display’ => __(‘Every four hours’)
);
!
return $schedules;
!
}
!
add_filter(‘cron_schedules’, ‘add_new_cron_intervals’);
7. Scheduling Recurring Tasks
!
!
!
function WeeklyMaintenanceTask_activate() {
// schedule the task
wp_schedule_event(time(), ‘weekly’, ‘WeeklyMaintenanceTask_run’);
}
register_activation_hook( __FILE__, 'WeeklyMaintenanceTask_activate' );
!
!
!
function WeeklyMaintenanceTask_run() {
// do maintenance task here
}
add_action(‘WeeklyMaintenanceTask_run’, ‘WeeklyMaintenanceTask_run’);
8. Cancelling Recurring Tasks
!
!
To clear all events related to a specified hook:
!
function WeeklyMaintenanceTask_deactivate() {
// clear scheduled tasks
wp_clear_scheduled_hook(‘WeeklyMaintenanceTask_run’);
}
register_deactivation_hook( __FILE__, 'WeeklyMaintenanceTask_deactivate' );
!
!
!
To clear only one event related to a specific hook:
(the difference - you need to know the timestamp of the event)
!
wp_unschedule_event(1399139100, ’WeeklyMaintenanceTask_run’);
9. Cancelling Recurring Tasks
!
!
When unscheduling/clearing events, the arguments you specified
during scheduling have to match!
!
wp_schedule_event(1399139100, ’MyHook’, array(1000));
!
!
When unscheduling/clearing events, the arguments you specified
during scheduling have to match!
!
wp_unschedule_event(1399139100, ’MyHook’);
10. Asynchronous Tasks
!
Perfect for long running tasks
!
Allows a user to continue working on your site
!
add_action(‘user_register’, ‘add_user_to_crm’, 10, 1);
!
function add_user_to_crm($user_id) {
$userId = get_current_user_id();
wp_schedule_event(time(), ‘event_addUserToCRM’, array($user_id));
}
!
function addUserToCRM($user_id) {
// insert slow running code here
}
add_action(‘addUserToCRM’, ‘addUserToCRM’);
11. Looking up a task
!
!
Perfect for avoiding duplicate events
!
wp_next_scheduled( $hook, $args );
!
This function returns the next scheduled cron event for the hook (and arguments)
selected.
!
If found, it will return the timestamp of the next event.
!
If there are no matches, it will return false.
12. Advantages of WP-Cron
!
!
It will work on all operating systems
!
!
!
!
!
!
!
!
!
No end user configuration required
13. Disadvantages of WP-Cron
!
Not a true Cron replacement
!
Only runs when someone visits your site
!
No visits = no jobs
!
!
!
Can use a lot of system resources on a busy site
!
!
Caching can cause events not to fire properly
19. Resources
!
Cron Functions in the WordPress Codex:
http://codex.wordpress.org/Function_Reference/wp_cron
!
Setting up a crontab file on the Unix server:
http://v1.corenominal.org/howto-setup-a-crontab-file/
!
EasyCron Web Service:
https://www.easycron.com/
!
Cron GUI WordPress Plugin:
http://wordpress.org/plugins/cron-view/
!
Epoch & Unix Time Conversion Tools:
http://www.epochconverter.com/
!
These slides:
http://www.5sen.se/wpcron