Cron is a Linux server service that is typically used to schedule commands or scripts on a web server that runs in the background. A cron job is the task itself, which is used to schedule tasks at periodic fixed dates, times, or intervals. Typically these involve repetitive maintenance tasks that are automated to save time.
Table of Contents
- Understanding WordPress Cron Job
- How to run a WordPress Cron Job
- Changing Cron Job schedules with WP-Crontrol plugin
- Adding an Event to run our Job
Understanding WordPress Cron Job
In WordPress, we have our own cron called WP-Cron, which is used to simulate a system cron.
WP-Cron, by default, only runs when someone visits your site.
Whenever someone visits your site, WP-Cron checks if there are any scheduled tasks to be run. If it finds that a task’s scheduled time has been met, it will go ahead and run that task.
If you are looking for performance, it’s a good idea to disable the WP-Cron internal process dispatcher and call it from an external service.
You can learn how to disable your WordPress cron in this post or get a FREE migration to a Wetopi WordPress specialized server. With wetopi your servers are tuned for performance and by default, call the wp-cron from an external service every 20 seconds.
How to run a WordPress Cron Job
To run a WordPress cron with WP-Cron events you have to create custom hooks.
If you are an advanced user we recommend the official WordPress Plugin Handbook guide on scheduling WP Cron events.
For those who perhaps aren’t as advanced, we will describe the process using the free WP Crontrol plugin.
WP Crontrol lets you view and control what’s happening in the WP-Cron system.
From the admin screens you can: View all cron events, add new ones, edit, delete, etc …
Changing Cron Job schedules with WP-Crontrol plugin
Once WP Crontrol plugin is installed and activated you can change the WordPress Cron job schedules under “Cron Schedules”. This option is within the settings of your WordPress admin:
You can add additional schedules in seconds, such as 600 seconds for every 10 minutes interval:
Adding an Event to run our Job
Click into “Cron Events.” under Tools:
Many of the action name’s of the events are easily recognizable as they should match part of the task name, such as “wp_update_plugins” or “wp_update_themes.”
In this how-to, we are going to To add a WordPress Cron job to make the simple task of sending an email.
This is a new action, so we will have to add the action and place the function code in our functions.php file:
add_action( 'send_mail', 'send_mail_func' );
function send_mail_func() {
wp_mail( 'hello@world.com', 'Sending an automated email', 'Hello World' );
}
Then we click on the “Add Cron Event.”
The Hook Name, must be the same we have added to functions “send_mail”, the we set the desired start Time in “Next Run” and the recurrence based in our Schedules.
You can run a Cron event hook immediately by simply clicking on the “Run Now” you’ll find in the last column. This can be helpful when troubleshooting things as you might need to run a Cron event multiple times.
You can also edit existing Cron events by clicking on “Edit” next to the action name.
You may also enjoy Disabling Wp-Cron.
Don’t you have an account on Wetopi?
Free full performance servers for your development and test.
No credit card required.