Updated on Nov 17, 2022
Cron is a Unix/Linux utility that is typically used to schedule background commands or scripts on a web server. A cron job is a task that is used to schedule tasks at set times, dates, or intervals. These are typically repetitive tasks that are automated to save time. WP-Cron, which simulates a system cron, handles this in WordPress.
A WordPress cron job might include scheduling a post to publish or running a backup plugin on a predefined schedule. Today, we'll look at how to easily create, modify, and run a WordPress cron job.
This post includes:
It is important to note that WP-Cron is slightly different than a system cron, and they each have their own pros and cons. For example, WP-Cron relies entirely on intervals, whereas a system cron relies on specific times. A WP-Cron also only works when the page is loaded (back-end or front-end), which could be less reliable. The default intervals provided by WordPress are hourly, twice daily, and daily.
You must create custom hooks in order to schedule WP-Cron events. We recommend reading the official WordPress plugin handbook, which includes an excellent guide to scheduling WP Cron events. If you want to set up a system cron with WordPress in a different way, we recommend reading Tom Mcfarlin's article on defining a WordPress cron job. And for those who aren't as advanced, we'll look at how to use WP-Cron in conjunction with the popular free WP Crontrol plugin. This allows you to see and control what's going on in the WP-Cron system.
WP Crontrol has over 200,000 active installations and a 4.9 out of 5 rating. This plugin is extremely popular among WordPress users.
This plugin is extremely useful. Shows what CRON jobs should run, which is an excellent first step in understanding what's going on and whether or not it's working. Right there, 5 stars. In addition, you can create new cron jobs and trigger existing ones to run, earning you 5 bonus stars. — Josh, Caldera Forms creator
You can get it from the WordPress repository or by searching for it in the "Add New" plugins section of your WordPress dashboard. WP Control includes the following features:
Once activated, you can modify the WordPress Cron job schedules in the "Cron Schedules" section of your WordPress dashboard's settings. It's worth noting that the plugin adds a weekly default schedule. Additional schedules in seconds, such as 21600 seconds every 6 hours, can also be added.
wp-cron schedules
WP-Cron schedules
These intervals can also be added with code using a filter, such as:
add_filter( 'cron_schedules', 'example_add_cron_interval' );
function example_add_cron_interval( $schedules ) {
$schedules['five_seconds'] = array(
'interval' => 5,
'display' => esc_html__( 'Every Five Seconds' ),
);
return $schedules;
}
You can then use the plugin to view your currently scheduled WordPress Cron jobs. Select "Cron Events" from the Tools menu. Many of the action names are easily recognizable because they should correspond to a part of the plugin name, such as "woocoomerce_cleanup_sessions” or “gravityforms_cron."
You can run a Cron event right away by clicking the "Run Now" button next to the action name. This is useful when troubleshooting because you may need to run a Cron event multiple times.
Existing Cron events can also be edited by clicking "Edit" next to the action name. The action name, arguments, next run, and scheduled recurrence can then be changed. However, be cautious because many plugins rely on Cron jobs to function properly.
Cron events can also be added. In this example, we'll set up a WordPress Cron job for the Disqus plugin. Many people may want to do this to change the sync frequency or if they are experiencing problems with comments syncing back and forth. The name of each third-party plugin developer's cron event, or rather the action name, should be included in their documentation. In this case, the plugin is making use of "dsq_sync_forum."
If you are creating one from scratch you will need a corresponding action hook somewhere in your code, such as your functions.php
file. This is the example WP-Crontrol gives:
add_action( 'my_hookname', 'my_function' );
The next step is to write your function.
function my_function() {
wp_mail( '[email protected]', 'WP Crontrol', 'WP Crontrol rocks!' );
}
In WP-CLI, you can also manage WP-Cron events or a WordPress Cron job. The following command, for example, will display your current cron event list.
Additional commands and parameters can be found in the official WP-CLI cron documentation.
To disable the default virtual WordPress cron job, follow these steps:
wp-config.php
file in a text editor. The wp-config.php
file is located in the directory where you installed WordPress. Usually, this is the public_html
directory.wp-config.php
file:define('DISABLE_WP_CRON', true);
wp-config.php
file:/* That's all, stop editing! Happy publishing. */
After you've disabled the default WordPress cron configuration in the wp-config.php file, you're ready to create a real cron job that runs at regular intervals regardless of site traffic.
You can use cPanel to configure the cron job if your account includes it. You can also configure the cron job from the command line.
To set up a WordPress cron job using cPanel, follow these steps:
>/dev/null 2>&1
to the command, which redirects all output to /dev/null
.cd ${HOME}/public_html; /usr/local/bin/php -q wp-cron.php
Cron jobs can be crucial for minimizing the things you need to do manually. Take advantage of that and create cron jobs for as many things as you can. We hope this guide was helpful enough so you can grasp the concept of WordPress cron jobs.
We hope you find this article useful. Discover more about FastCloud - the top-rated Hosting Solutions for personal and small business websites in four consecutive years by the HostAdvice Community!