Cyber Week Deals! Save 80% on all Shared Hosting Plans & Locations - Starting from $1.99/mo

How to Create and Modify a WordPress Cron Job

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:

How to Set up a WordPress Cron Job

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:

  • Ability to view all cron events along with their arguments, recurrence, and next run time.
  • Edit, delete, and immediately run any cron events.
  • Add new cron events.
  • Add, edit, and remove custom cron schedules.

WP-Cron Schedules

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;
}

WP-Cron Events

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.

Modify Cron Event

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.

Add Cron Event

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!' );
}

WP-Cron WP-CLI

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.

How to Disable the Virtual WordPress Cron Job

To disable the default virtual WordPress cron job, follow these steps:

  • Log in to your cPanel account .
  • Using the cPanel File Manager, open the 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.
  • Add the following line to the wp-config.php file:
define('DISABLE_WP_CRON', true);
  • Make sure you add the previous line before the following line in the wp-config.php file:
/* That's all, stop editing! Happy publishing. */
  • Save the changes to the wp-config.php file and then exit the text editor. The virtual WordPress cron job is now disabled.

How to Configure a Real Cron job for WordPress via cPanel

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:

  • Log in to cPanel.
  • In the Advanced section of the cPanel home screen, click Cron jobs.
  • Under Cron Email, type the e-mail address that you want to receive notifications, and then click Update Email. Every time the cron job runs, the e-mail account will receive a message.
  • If you do not want to receive e-mail notifications for the cron job, you can append >/dev/null 2>&1 to the command, which redirects all output to /dev/null.
  • Under Add New Cron Job, in the Common Settings list box, select Twice an hour. You can run cron jobs a maximum of every 15 minutes on shared and reseller accounts. A 30-minute interval for the WordPress cron job should be more than sufficient.
  • In the Command text box, type the following line:
cd ${HOME}/public_html; /usr/local/bin/php -q wp-cron.php
  • This line assumes you installed WordPress in the document root (public_html) directory. If you installed WordPress in another directory, modify the cd command to change to that directory instead.
  • Click Add New Cron Job. The new cron job settings take effect immediately.

Conclusion

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!

WordPress Hosting

  • Free WordPress Installation
  • 24/7 WordPress Support
  • Free Domain Transfer
  • Hack-free Protection
  • Fast SSD Storage
  • Free WordPress Transfer
  • Free CloudFlare CDN
  • Immediate Activation
View More