Time Code: Automating
Tasks in WordPress with
WP-Cron
WordCamp Ottawa
Saturday, May 3, 2014
What is WP-Cron?
!
A task scheduler built into WordPress.
!
Named after the Unix Cron utility:
!
“The software utility cro...
Scheduling a Single Event
!
The wp_schedule_single_event() function is used to schedule an event to run once.
This could b...
Scheduling Recurring Tasks
!
WP-Cron can run tasks at regular intervals
!
In the core:
!
• Checking if WordPress version i...
Schedules
Out of the box, WordPress can run recurring tasks:
!
• Daily (once every 24 hours)
• Twice Daily (every 12 hours...
Need Another Schedule?
You can create new schedules using the cron_schedules filter.
!
function add_new_cron_intervals($sch...
Scheduling Recurring Tasks
!
!
!
function WeeklyMaintenanceTask_activate() {
	 // schedule the task

	 wp_schedule_event(t...
Cancelling Recurring Tasks
!
!
To clear all events related to a specified hook:
!
function WeeklyMaintenanceTask_deactivate...
Cancelling Recurring Tasks
!
!
When unscheduling/clearing events, the arguments you specified
during scheduling have to mat...
Asynchronous Tasks
!
Perfect for long running tasks
!
Allows a user to continue working on your site
!
add_action(‘user_re...
Looking up a task
!
!
Perfect for avoiding duplicate events
!
wp_next_scheduled( $hook, $args );
!
This function returns t...
Advantages of WP-Cron
!
!
It will work on all operating systems
!
!
!
!
!
!
!
!
!
No end user configuration required
Disadvantages of WP-Cron
!
Not a true Cron replacement
!
	 Only runs when someone visits your site
!
	 No visits = no jobs...
Disabling WP-Cron
!
!
Add this to your wp-config.php file:
!
define(‘DISABLE_WP_CRON’, true);
Setting up Cron in cPanel
wget -q -O - http://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
Cron GUI by Simon Wheatley
Sample API Integration
Sample API Integration
!
Resources
!
Cron Functions in the WordPress Codex:
http://codex.wordpress.org/Function_Reference/wp_cron
!
Setting up a cr...
exit;
!
Thank you!
!
Questions?
!
shawn@fivesense.ca
!
Twitter: @shawnhooper
!
Slides:
http://5sen.se/wpcron
!
Upcoming SlideShare
Loading in …5
×

Time Code: Automating Tasks in WordPress with WP-Cron

2,131 views

Published on

Slides for my WordCamp Ottawa 2014 Presentation. Describes managed timed events from within WordPress

Published in: Internet, Technology, Business

Time Code: Automating Tasks in WordPress with WP-Cron

  1. 1. Time Code: Automating Tasks in WordPress with WP-Cron WordCamp Ottawa Saturday, May 3, 2014
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 12. Advantages of WP-Cron ! ! It will work on all operating systems ! ! ! ! ! ! ! ! ! No end user configuration required
  13. 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
  14. 14. Disabling WP-Cron ! ! Add this to your wp-config.php file: ! define(‘DISABLE_WP_CRON’, true);
  15. 15. Setting up Cron in cPanel wget -q -O - http://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
  16. 16. Cron GUI by Simon Wheatley
  17. 17. Sample API Integration
  18. 18. Sample API Integration !
  19. 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
  20. 20. exit; ! Thank you! ! Questions? ! shawn@fivesense.ca ! Twitter: @shawnhooper ! Slides: http://5sen.se/wpcron !

×