Cron is a unix/Linux utility that is typically used to schedule commands or a script 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 times, dates, or intervals. Typically these involve repetitive tasks that are automated to save time. In WordPress this is handled by WP-Cron, which is used to simulate a system cron. Some examples of a WordPress cron job might involve scheduling a post to publish or a backup plugin running on a predefined schedule. Today we are going to dive into how to easily create, modify, and run a WordPress cron job.

wordpress cron job

Setup 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, twicedaily, and daily.

To schedule WP-Cron events you have to create custom hooks. We recommend checking out the official WordPress plugin handbook which has an amazing guide on scheduling WP Cron events. If you are looking on an alternative way to setup a system cron with WordPress we recommend checking out Tom Mcfarlin’s article on defining a WordPress cron job. And for those who perhaps aren’t as advanced, we are going to explore how to easily use WP-Cron along with the popular free WP Crontrol plugin. This lets you view and control what’s happening in the WP-Cron system.

wp control plugin

WP Crontrol has over 40,000 active installs with an impressive 4.9 out of 5 rating. Many people in the WordPress community really love this plugin.

This plugin is super useful. Shows what CRON jobs should run, excellent first step in understanding what’s going on and if it’s working or not. 5 Stars right there. Also, you can create new cron jobs and trigger existing ones to run, 5 bonus stars on top. — Josh, creator of Caldera Forms

You can download it from the WordPress repository or by searching for it within your WordPress dashboard under “Add New” plugins.  Features of WP Crontrol include:

  • 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 change the WordPress Cron job schedules under “Cron Schedules” from within the settings of your WordPress dashboard. Notice that the plugin adds an additional default schedule of weekly. You can also add additional schedules in seconds, such as 21600 seconds for every 6 hours.

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 also use the plugin to see your current WordPress Cron jobs that are already scheduled. Under Tools click into “Cron Events.” Many of the action name’s are easily recognizable as they should match part of the plugin name, such as “woocoomerce_cleanup_sessions” or “gravityforms_cron.”

wp-cron events

You run a Cron event immediately by simply clicking on “Run Now” next to the action name. This can be helpful when troubleshooting things as you might need to run a Cron event multiple times.

successfully excuted cron event

Modify Cron Event

You can also edit existing Cron events by clicking on “Edit” next to the action name. You can then change the action name, arguments, the next run, and scheduled recurrence. Be careful with this though as many plugins rely on their Cron jobs to function properly.

modify cron event

Add Cron Event

You can also Add Cron events. In this example, we are going to add the WordPress Cron job for the Disqus plugin. Many might want to do this to alter the sync frequency or if they are having issues with comments syncing back and forth. Each third party plugin developer’s documentation should have the name of their cron event, or rather the action name. In this case it is “dsq_sync_forum” which is being used by the plugin.

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( 'hello@example.com', 'WP Crontrol', 'WP Crontrol rocks!' );
}

For Disqus, we first add a new cron schedule of 600 seconds (10 minutes). We then click into “Add Cron Event” and input “dsq_sync_forum” as our action name, choose the next run time, and choose our 10 minute recurrence we just created. Then we click on “Add Cron Event.” This will now run every 10 minutes automatically for us to manually sync Disqus comments back to our WordPress database.

add WordPress cron job

And that’s about it! It is really pretty easy to use.

WP-Cron WP-CLI

You can also manage WP-Cron events and or a WordPress Cron job in WP-CLI. For example, the following command will give you your current cron event list.

wp cron event list

wp-cron event list

See the official WP-CLI cron documentation for additional commands and parameters.

Related Articles

Haven't Found What You Need?

Head back to the knowledge base or open a support ticket

Back To The Knowledge Base Open A Support Ticket
email newsletter

Use WordPress?

Join 15,000+ others who get our FREE weekly newsletter with WordPress tips on how to drive more traffic and revenue to your business!

You have Successfully Subscribed!

Send this to a friend