Automating your plugin with WP-Cron

20
Automating Your Plugins With WP-Cron WordCamp Savannah 2010 Dan Cannon @NicasioDesign http://nicasiodesign.com

description

Presentation for WordCamp Savannah on using the WP-Cron set of functions to automate tasks in WordPress plugins. See http://bit.ly/bhZyYU to download the example plugin used in the presentation.

Transcript of Automating your plugin with WP-Cron

Page 1: Automating your plugin with WP-Cron

Automating Your PluginsWith WP-Cron

WordCamp Savannah 2010

Dan Cannon

@NicasioDesign

http://nicasiodesign.com

Page 2: Automating your plugin with WP-Cron

Overview

• Background and General Info

• Code

• Plugin File/Demo

• Pros & Cons

• Practical Uses

• Conclusion

• Demo Plugin Used in Presentation:http://bit.ly/bhZyYU

Page 3: Automating your plugin with WP-Cron

What is a Traditional Cron Job?

• “Cron is a time-based job scheduler in Unix-like computer operating systems. The name cron comes from the word ‘chronos’, Greek for ‘time’. Cron enables users to schedule jobs … to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes…”- Wikipedia (http://en.wikipedia.org/wiki/Cron_job)

Page 4: Automating your plugin with WP-Cron

What is WP-Cron?

• WP-Cron is a pseudo Cron Job– Executed after a certain time, the next time

someone visits the site.

• Controlled by a group of WordPress functions

• Usually implemented in plugins that need to automate a task

Page 5: Automating your plugin with WP-Cron

WP-Cron vs Traditional Cron

• Why use WP-Cron– Setting up traditional cron jobs can vary

depending on server OS and settings

– Don’t want to tell plugin users they have to setup a cron job to use your plugin

• Most won’t even know where to begin

Page 6: Automating your plugin with WP-Cron

Schedule a Recurring Event

wp_schedule_event($timestamp,$recurrence,$hook,$args)

– $timestamp = UNIX timestamp we want to start the job• Usually PHP time() function

– $recurrence = hourly, twicedaily, daily• We can add our own schedules (covered later)

– $hook = the hook we want to be called on execution– $args = arguments to pass to the hook’s function (optional)

Page 7: Automating your plugin with WP-Cron

Schedule a Recurring Event (cont.)

Schedule Event on Plugin Activation

http://codex.wordpress.org/Function_Reference/wp_schedule_eventhttp://codex.wordpress.org/Function_Reference/register_activation_hook

Page 8: Automating your plugin with WP-Cron

Schedule a Recurring Event (cont.)

The actual function we want to run once an hour

http://codex.wordpress.org/Function_Reference/wp_insert_post

Page 9: Automating your plugin with WP-Cron

Schedule a Recurring Event (cont.)

We need to clear our job when the plugin is deactivated

http://codex.wordpress.org/Function_Reference/wp_clear_scheduled_hookhttp://codex.wordpress.org/Function_Reference/register_deactivation_hook

Page 10: Automating your plugin with WP-Cron

Cron Schedule• Three Default recurrence values as of WordPress 3.0.1

– Once Hourly– Twice Daily– Once Daily

• Output of wp_get_schedules()

http://codex.wordpress.org/Function_Reference/wp_get_schedules

Page 11: Automating your plugin with WP-Cron

Cron Schedule

Add Our Own Schedules

http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules

Page 12: Automating your plugin with WP-Cron

Use Custom Schedules

Page 13: Automating your plugin with WP-Cron

Schedule a Single Event

• wp_schedule_single_event( $timestamp, $hook, $args )

– $timestamp – UNIX timestamp that you want event to run at

– $hook – hook that should be called at proper time

– $args – (optional) arguments to pass to the hook’s function

http://codex.wordpress.org/Function_Reference/wp_schedule_single_event

Page 14: Automating your plugin with WP-Cron

Schedule a Single EventSimilar Syntax/Implementation to recurring events

Page 15: Automating your plugin with WP-Cron

Avoid Redundant Jobs• Use wp_next_scheduled to see if an event is already scheduled to avoid setting up redundant

jobs• Useful for events declared in theme functions file, or events scheduled on the fly

• Will return timestamp for next event for a given hook

http://codex.wordpress.org/Function_Reference/wp_next_scheduled

Page 16: Automating your plugin with WP-Cron

Pros

• WordPress Standard– Will still work after upgrades (in theory)

• System Independent– Works on all flavors of Linux and Windows

• Makes life easy for end users– No messy system or hosting settings to

change

Page 17: Automating your plugin with WP-Cron

Cons• Not Precise

– Will run after a certain point, the next time someone visits the site

• No Traffic = No Job– If no one visits your site, your job won’t run

• Tip: Use a service like WebCron.org to make sure important jobs get executed

Page 18: Automating your plugin with WP-Cron

Cons (cont.)

• Can be hard on server resources– Multiple jobs running often can be hard on your server

resources.• Can affect your site in different ways depending on your

hosting provider/server

• Can be Problematic with Caching– If your site uses caching then WP-Cron jobs will only

run on page loads where your cache is rebuilt for that page

– Could mean fewer executions for your jobs depending on frequency and traffic volume.

Page 19: Automating your plugin with WP-Cron

Practical Uses• API Integration

– Help cut down external API calls by storing API data periodically

• Database and Site Maintenance

• Mailing Lists and Reminders– Break up large lists over a long period of time

• Content Aggregation

• Any Others?

Page 20: Automating your plugin with WP-Cron

Conclusion

• WP-Cron functions provide powerful automation for WordPress plugins, but may not be right for every situation requiring automation.

Dan Cannon

@NicasioDesign

http://nicasiodesign.com