When I say scheduled events, I don’t mean the awesome party that you may want to sell on your site, no. The scheduled events I’m talking about are actually events that occur in WordPress.
An example would be a scheduled sale on WooCommerce, or a scheduled post in your blog that you want to publish in the future.
You can create such events also by yourself, for your theme or your plugin. Like I said before, WooCommerce uses scheduled events for sales, that’s a custom one. I used them to sync two different blogs with a plugin.
If you want to learn how to use scheduled events in WordPress, keep reading!
Scheduled Events vs. Cron Jobs
You should know the difference between a scheduled event and a cron job.
Usually, WordPress people call scheduled events simply WordPress Cron Jobs, and they are not totally wrong. But real cron jobs are a different thing.
Real cron jobs run at server level, and you can configure them on your host/machine. They are independent from your site and work regardless of how many people visits your site.
WordPress cron jobs instead, or scheduled events, are like cron jobs, but they run in WordPress. Like real cron jobs, they can run at predefined intervals, like every day, every week, etc.
The bad thing about them is that, since they rely on WordPress to work, people need to visit your site. If nobody is visiting your site WordPress does nothing, and so scheduled events won’t run.
Make Sure That Scheduled Events Always Run
There’s a way to make sure that your scheduled events run regardless of the visits on your site, and it’s by using robots to ping WordPress.
This way scheduled events will always run, even if there are not real visits.
Create Scheduled Events
To create a scheduled event you need the WordPress function
It accepts three parameters:
- When to run the event for the first time;
- The frequency of the event;
- A hook name.
The first parameters is a timestamp indicating when you want the event to run for the first time.
The second parameter, is a string that indicates how often it should run. It can
be one of the following values:
You can also add a custom interval, but I will explain how to do that in a different article.
The third parameter, is a custom hook name.
Write The Code
Let’s get our hands dirty with some code. In the example we will create a scheduled event that runs every day.
Create a folder in wp-content/plugins/ and name it example-scheduled-events. Then create a file inside of it named example-scheduled-event.php, open it and add this code at the beginning of the file:
At the beginning of the file we write the headers of the plugin, or WordPress won’t see it.
Right after that, we create an activation hook that runs only once, when you activate the plugin. The function hooked will check if the event is already scheduled, and if it is not, it creates the event and schedules it to run every hour.
It also specifies a function to run for that event, which is right after the activation hook. It simply writes a sentence in the error log.
The last function clears the schedule when you disable the plugin and runs only once.
You can now activate the plugin to see how it works. Once active, check your site’s error log, you will find the message “I’m alive!” inside of it. Check it one hour later, and you will find the same message again, and so on every hour.
Debugging Scheduled Events
The best way to debug scheduled events is to use
error_log() instead, printing the output in the debug.log file in your wp-content/ folder.