Time Code: Automating Tasks in WordPress with WP-Cron

Post on 23-Aug-2014

817 views 3 download

Tags:

description

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

Transcript of Time Code: Automating Tasks in WordPress with WP-Cron

Time Code: Automating Tasks in WordPress with

WP-Cron

WordCamp Ottawa Saturday, May 3, 2014

What is WP-Cron? !A task schedule 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 !!WP-Cron is a pseudo Cron utility

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

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)

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’);

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’);

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’);

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’);

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’); !!

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’);

Cron GUI by Simon Wheatley

Advantages of WP-Cron !Keeps all functionality inside the WordPress environment !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://yourwebsite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

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

exit; !

Thank you! !

Questions? !

shawn@fivesense.ca !

Twitter: @shawnhooper !

Slides: http://5sen.se/wpcron

!