Chapter 13. Cron

WHAT'S IN THIS CHAPTER?

  • Understanding cron

  • Managing schedule and single events

  • Unscheduling cron events

  • Viewing all scheduled cron jobs

  • Creating custom intervals in cron

  • Creating practical use examples

The execution of certain functions on a schedule is a key feature of the WordPress API. In this chapter you learn how to schedule events in cron, unschedule events, and set custom cron schedule intervals. You also create more advanced, practical-use example plugins using cron.

WHAT IS CRON?

Cron is how WordPress handles scheduled events. The term cron comes from the time-based job scheduler in UNIX. WordPress uses cron for various core functionality. These scheduled jobs include checking for new versions of WordPress, checking for plugin and theme updates, and publishing scheduled posts.

How Is Cron Executed?

One of the common misconceptions about cron in WordPress is that cron is always running, looking for tasks to execute. This actually isn't true. Cron is run when a frontend or admin page is loaded on your web site. Every time a page is requested, WordPress checks if there are any cron jobs to run. Any visit to your Web site can trigger cron, whether from a visitor or a search engine bot.

This is also one of the caveats of cron. Because cron runs on page load, it is not 100% precise. If you have a scheduled cron job to run at midnight, but your Web site lacks adequate traffic, the scheduled job may not run until 12:30 a.m. or later because no one is on your Web site that ...

Get Professional WordPress® Plugin Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.