Upload
dan-cannon
View
13.459
Download
2
Embed Size (px)
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.
Citation preview
Automating Your PluginsWith WP-Cron
WordCamp Savannah 2010
Dan Cannon
@NicasioDesign
http://nicasiodesign.com
Overview
• Background and General Info
• Code
• Plugin File/Demo
• Pros & Cons
• Practical Uses
• Conclusion
• Demo Plugin Used in Presentation:http://bit.ly/bhZyYU
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)
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
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
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)
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
Schedule a Recurring Event (cont.)
The actual function we want to run once an hour
http://codex.wordpress.org/Function_Reference/wp_insert_post
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
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
Cron Schedule
Add Our Own Schedules
http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
Use Custom Schedules
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
Schedule a Single EventSimilar Syntax/Implementation to recurring events
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
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
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
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.
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?
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