Download - Automating your plugin with WP-Cron
![Page 1: Automating your plugin with WP-Cron](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/1.jpg)
Automating Your PluginsWith WP-Cron
WordCamp Savannah 2010
Dan Cannon
@NicasioDesign
http://nicasiodesign.com
![Page 2: Automating your plugin with WP-Cron](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/11.jpg)
Cron Schedule
Add Our Own Schedules
http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
![Page 12: Automating your plugin with WP-Cron](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/12.jpg)
Use Custom Schedules
![Page 13: Automating your plugin with WP-Cron](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/14.jpg)
Schedule a Single EventSimilar Syntax/Implementation to recurring events
![Page 15: Automating your plugin with WP-Cron](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052504/554bc3f9b4c90594278b535e/html5/thumbnails/20.jpg)
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