Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Automating Your Plugins With WP-Cron WordCamp Savannah 2010 Dan Cannon @ NicasioDesign http://nicasiodesign.com
Overview <ul><li>Background and General Info </li></ul><ul><li>Code </li></ul><ul><li>Plugin File/Demo </li></ul><ul><li>P...
What is a Traditional Cron Job? <ul><li>“ Cron is a time-based job scheduler in Unix-like computer operating systems. The ...
What is WP-Cron? <ul><li>WP-Cron is a pseudo Cron Job </li></ul><ul><ul><li>Executed after a certain time, the next time s...
WP-Cron vs Traditional Cron <ul><li>Why use WP-Cron </li></ul><ul><ul><li>Setting up traditional cron jobs can vary depend...
Schedule a Recurring Event   <ul><li>wp_schedule_event($timestamp,$recurrence,$hook,$args) </li></ul><ul><ul><li>$timestam...
Schedule a Recurring Event (cont.) Schedule Event on Plugin Activation http://codex.wordpress.org/Function_Reference/wp_sc...
Schedule a Recurring Event (cont.) The actual function we want to run once an hour http://codex.wordpress.org/Function_Ref...
Schedule a Recurring Event (cont.) <ul><li>We need to clear our job when the plugin is deactivated </li></ul>http://codex....
Cron Schedule <ul><li>Three Default recurrence values as of WordPress 3.0.1 </li></ul><ul><ul><li>Once Hourly </li></ul></...
Cron Schedule <ul><li>Add Our Own Schedules </li></ul>http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
Use Custom Schedules
Schedule a Single Event <ul><li>wp_schedule_single_event( $timestamp, $hook, $args ) </li></ul><ul><ul><li>$timestamp – UN...
Schedule a Single Event <ul><li>Similar Syntax/Implementation to recurring events </li></ul>
Avoid Redundant Jobs <ul><li>Use wp_next_scheduled to see if an event is already scheduled to avoid setting up redundant j...
Pros <ul><li>WordPress Standard </li></ul><ul><ul><li>Will still work after upgrades (in theory) </li></ul></ul><ul><li>Sy...
Cons <ul><li>Not Precise </li></ul><ul><ul><li>Will run  after  a certain point, the next time someone visits the site </l...
Cons (cont.) <ul><li>Can be hard on server resources </li></ul><ul><ul><li>Multiple jobs running often can be hard on your...
Practical Uses <ul><li>API Integration </li></ul><ul><ul><li>Help cut down external API calls by storing API data periodic...
Conclusion <ul><li>WP-Cron functions provide powerful automation for WordPress plugins, but may not be right for every sit...
Upcoming SlideShare
Loading in …5
×

Automating your plugin with WP-Cron

15,101 views

Published on

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.

Published in: Technology
  • Login to see the comments

Automating your plugin with WP-Cron

  1. 1. Automating Your Plugins With WP-Cron WordCamp Savannah 2010 Dan Cannon @ NicasioDesign http://nicasiodesign.com
  2. 2. Overview <ul><li>Background and General Info </li></ul><ul><li>Code </li></ul><ul><li>Plugin File/Demo </li></ul><ul><li>Pros & Cons </li></ul><ul><li>Practical Uses </li></ul><ul><li>Conclusion </li></ul><ul><li>Demo Plugin Used in Presentation: </li></ul><ul><ul><li>http://bit.ly/bhZyYU </li></ul></ul>
  3. 3. What is a Traditional Cron Job? <ul><li>“ 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) </li></ul>
  4. 4. What is WP-Cron? <ul><li>WP-Cron is a pseudo Cron Job </li></ul><ul><ul><li>Executed after a certain time, the next time someone visits the site. </li></ul></ul><ul><li>Controlled by a group of WordPress functions </li></ul><ul><li>Usually implemented in plugins that need to automate a task </li></ul>
  5. 5. WP-Cron vs Traditional Cron <ul><li>Why use WP-Cron </li></ul><ul><ul><li>Setting up traditional cron jobs can vary depending on server OS and settings </li></ul></ul><ul><ul><li>Don’t want to tell plugin users they have to setup a cron job to use your plugin </li></ul></ul><ul><ul><ul><li>Most won’t even know where to begin </li></ul></ul></ul>
  6. 6. Schedule a Recurring Event <ul><li>wp_schedule_event($timestamp,$recurrence,$hook,$args) </li></ul><ul><ul><li>$timestamp = UNIX timestamp we want to start the job </li></ul></ul><ul><ul><ul><li>Usually PHP time() function </li></ul></ul></ul><ul><ul><li>$recurrence = hourly, twicedaily, daily </li></ul></ul><ul><ul><ul><li>We can add our own schedules (covered later) </li></ul></ul></ul><ul><ul><li>$hook = the hook we want to be called on execution </li></ul></ul><ul><ul><li>$args = arguments to pass to the hook’s function (optional) </li></ul></ul>
  7. 7. Schedule a Recurring Event (cont.) Schedule Event on Plugin Activation http://codex.wordpress.org/Function_Reference/wp_schedule_event http://codex.wordpress.org/Function_Reference/register_activation_hook
  8. 8. Schedule a Recurring Event (cont.) The actual function we want to run once an hour http://codex.wordpress.org/Function_Reference/wp_insert_post
  9. 9. Schedule a Recurring Event (cont.) <ul><li>We need to clear our job when the plugin is deactivated </li></ul>http://codex.wordpress.org/Function_Reference/wp_clear_scheduled_hook http://codex.wordpress.org/Function_Reference/register_deactivation_hook
  10. 10. Cron Schedule <ul><li>Three Default recurrence values as of WordPress 3.0.1 </li></ul><ul><ul><li>Once Hourly </li></ul></ul><ul><ul><li>Twice Daily </li></ul></ul><ul><ul><li>Once Daily </li></ul></ul><ul><li>Output of wp_get_schedules() </li></ul>http://codex.wordpress.org/Function_Reference/wp_get_schedules
  11. 11. Cron Schedule <ul><li>Add Our Own Schedules </li></ul>http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
  12. 12. Use Custom Schedules
  13. 13. Schedule a Single Event <ul><li>wp_schedule_single_event( $timestamp, $hook, $args ) </li></ul><ul><ul><li>$timestamp – UNIX timestamp that you want event to run at </li></ul></ul><ul><ul><li>$hook – hook that should be called at proper time </li></ul></ul><ul><ul><li>$args – (optional) arguments to pass to the hook’s function </li></ul></ul>http://codex.wordpress.org/Function_Reference/wp_schedule_single_event
  14. 14. Schedule a Single Event <ul><li>Similar Syntax/Implementation to recurring events </li></ul>
  15. 15. Avoid Redundant Jobs <ul><li>Use wp_next_scheduled to see if an event is already scheduled to avoid setting up redundant jobs </li></ul><ul><li>Useful for events declared in theme functions file, or events scheduled on the fly </li></ul><ul><li>Will return timestamp for next event for a given hook </li></ul><ul><li>http://codex.wordpress.org/Function_Reference/wp_next_scheduled </li></ul>
  16. 16. Pros <ul><li>WordPress Standard </li></ul><ul><ul><li>Will still work after upgrades (in theory) </li></ul></ul><ul><li>System Independent </li></ul><ul><ul><li>Works on all flavors of Linux and Windows </li></ul></ul><ul><li>Makes life easy for end users </li></ul><ul><ul><li>No messy system or hosting settings to change </li></ul></ul>
  17. 17. Cons <ul><li>Not Precise </li></ul><ul><ul><li>Will run after a certain point, the next time someone visits the site </li></ul></ul><ul><li>No Traffic = No Job </li></ul><ul><ul><li>If no one visits your site, your job won’t run </li></ul></ul><ul><ul><ul><li>Tip: Use a service like WebCron.org to make sure important jobs get executed </li></ul></ul></ul>
  18. 18. Cons (cont.) <ul><li>Can be hard on server resources </li></ul><ul><ul><li>Multiple jobs running often can be hard on your server resources. </li></ul></ul><ul><ul><ul><li>Can affect your site in different ways depending on your hosting provider/server </li></ul></ul></ul><ul><li>Can be Problematic with Caching </li></ul><ul><ul><li>If your site uses caching then WP-Cron jobs will only run on page loads where your cache is rebuilt for that page </li></ul></ul><ul><ul><li>Could mean fewer executions for your jobs depending on frequency and traffic volume. </li></ul></ul>
  19. 19. Practical Uses <ul><li>API Integration </li></ul><ul><ul><li>Help cut down external API calls by storing API data periodically </li></ul></ul><ul><li>Database and Site Maintenance </li></ul><ul><li>Mailing Lists and Reminders </li></ul><ul><ul><li>Break up large lists over a long period of time </li></ul></ul><ul><li>Content Aggregation </li></ul><ul><li>Any Others? </li></ul>
  20. 20. Conclusion <ul><li>WP-Cron functions provide powerful automation for WordPress plugins, but may not be right for every situation requiring automation. </li></ul><ul><li>Dan Cannon </li></ul><ul><li>@ NicasioDesign </li></ul><ul><li>http://nicasiodesign.com </li></ul>

×