How to use WP-Cron
Ben Lobaugh
@benlobaugh
Avid sailor Washington native Currently reside in Seattle 1965 VW Bus Pilot Head of Development at FreshMuse Seattle WordPress Meetup & WordCamp Organizer
Session Overview
What is WP-Cron and why use it? Simple testing of your code
Understanding WP-Cron intervals How to schedule a recurring task
Unscheduling tasks
Viewing scheduled tasks
How to hook WP-Cron into the system cron
What is cron?
Cron is a time-based task scheduling system commonly available on UNIX system. There is also a Windows
version
• Cron runs continuously while the OS is on
• Tasks are scheduled to run at specific times. I.E.
Every hour at 5 minutes past the hour.
• Tasks may be missed if the system (or cron) is not
What is WP-Cron?
WP-Cron is the WordPress time-based task scheduler. WP-Cron runs on each page load and looks for tasks
Pros of WP-Cron
• Already integrated with WordPress and has a simple
API
• Many hosts (shared) do not allow access to the
system cron. This method still allows for time-based execution of tasks.
• Can be hooked into system cron for a more realistic
cron experience
Cons of WP-Cron
• Does not run continuously, only on page load
• WordPress makes an HTTP call to wp-cron.php. Some
server firewalls may block this type of external HTTP request
• High traffic sites may see WP-Cron using lots of resources.
This is a case where hooking WP-Cron to the system cron is a good idea
• Site caching may prevent WP-Cron from running until the
Understanding intervals
Unlike a traditional cron, WP-Cron does not schedule tasks for specific times. Instead, WP-Cron is given a first execution time and an interval in seconds to
execute the task again.
Example: A task scheduled for June 2nd, 2012 at 2:00 PM with a 300 second interval will fire at 2:00, 2:05, 2:10, etc
Built-in WP-Cron intervals
By default WP-Cron comes with only 3 built-in intervals
• hourly
• twice daily • daily
Adding additional intervals
In true WordPress fashion, there is a hook to add new intervals to WP-Cron. This hook is the ‘cron_schedules` filter.
{
add_filter( 'cron_schedules', 'bl_add_cron_intervals' );}
function bl_add_cron_intervals( $schedules ) {
$schedules['5seconds'] = array( 'interval' => 5,
'display' => __('Every 5 Seconds') );
Schedule a recurring task
The custom hook
When WP-Cron executes a task it does so by calling a
custom hook. You define this hook. Creating a new hook is simple and requires 2 parameters:
• Hook name
• Function to call
}
}
Schedule a recurring task
Check the schedule
It is very important to check if an event is scheduled
before scheduling it again. Failure to do so may result in hundreds of the same task scheduled!
wp_next_scheduled() takes the task name as a
parameter and returns false if not scheduled, or the timestamp of the next execution
Schedule a recurring task
Adding the task
Adding the new task is as simple as calling
wp_schedule_event() with the following 3 parameters:
• $timestamp - UNIX timestamp of the first execution
• $recurrence - Name of the interval (NOTE: Name, NOT
a time)
• $hook - Name of the custom hook to call
Unscheduling tasks
When a task is no longer needed it should be unscheduled. Please always unschedule tasks on plugin deactivation.
wp_unschedule_event() unschedules a task and all future
occurences from a given execution timestamp. The following two parameters are required:
• $timestamp - Timestamp of next task occurrence • $hook - Name of the custom hook for the task
View Scheduled Tasks
• Zack Tollman (@tollmanz) has created a great Debug Bar
If you want a more true cron-like experience (or freed up server resources), WP-Cron can be hooked into the system cron with the following steps:
• Disable WP-Cron execution on page load in wp-config.php
with
• Setup your system task scheduler and use a program such
as wget to call your wp-cron.php file with a command such
Hooking WP-Cron to system cron
}
}
}
define('DISABLE_WP_CRON', true);Live Code Demo
Any questions before the demo?
Code for the demo, along with a full tutorial, can be found at
Questions?
Ben Lobaugh
@benlobaugh