This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

WordPress Cron Jobs: What Are They and How to Create and Manage Them for Task Scheduling

Updated on September 29, 2022

9 Min Read
wordpress cron job

Many WordPress developers, especially beginners, have common questions, “what is WordPress cron” and “how to manage WordPress cron jobs?” The answer is simple!

Cron is a standard UNIX utility for scheduling task execution (script or command) at a specific time, date, or interval. The task it will execute is known as a cron job. As you can guess, the purpose of a cron job is to automate repetitive tasks so that you can use your time more productively.

However, the WordPress cron job is slightly different from the standard cron, so before diving into action and process, you need to understand what a WordPress cron is and how it works for task automation.

What Is WordPress Cron?

WordPress has its own cron system for scheduling tasks such as checking for updates, scheduling a post for publication, and deleting comments from the trash. All cron jobs are handled by WP-Cron.

While the name (and the main idea) comes from UNIX cron, WordPress cron doesn’t work like that and uses intervals for task scheduling. The default time intervals provided by WordPress are hourly, twice daily, daily, and weekly. Here, the time-based tasks depend on users visiting your site, meaning that WordPress cron will only execute if a user visits your website.

In other words, WordPress cron is executed when the page loads up. Sometimes this isn’t that reliable, and that’s why many users prefer a server-level cron job over the WP-Cron job.

Advantages of WordPress Cron

If you want to automate a website process or need to check, verify, and control it regularly, cron jobs come in handy. For example, we might want to ping a server once a day, check the status of a system every hour (and log it into the database), and so on.

WP Cron makes it easy for the WordPress core and other plugins to perform time-based tasks. It is relatively easy to set up a WordPress Cron job instead of using a real cron job that requires knowledge of Linux commands.

Also, because the WP-Cron uses intervals to schedule tasks, you can rest assured that your task will eventually run even if the scheduled time elapses, unlike a real cron that specifies the exact time a task should run. If the time passes and the task fails to run, then it will not run again until you manually reschedule it.

Don’t Waste Your Time in Performing Manual Tasks in WordPress.

Try out Cloudways Platform, which has an integrated Cron Job Management for automated task scheduling in WordPress.

How to Manage WordPress Cron Jobs

There are many ways to set up and manage WordPress cron jobs, but I’ll show you two popular methods.

The first uses a cron management plugin like WP Crontrol that allows you to add, modify, and manage WordPress cron jobs directly from the WP dashboard. You can also create a cron job, build hooks and functions, and define custom time intervals for tasks.

And the second method is with WordPress CLI.

How to Manage WordPress Cron Jobs With Plugins

I’ll use the WP Crontrol plugin that allows you to view and control WordPress cron for your WordPress application. It is the popular plugin that handles WP cron jobs and has more than 200,000+ active installations.

WP-Cron does not run continuously. By default, the wp-cron.php fires on each page load, which on high-traffic websites can cause issues. If a website doesn’t have sufficient PHP workers, sometimes a request will come in, and WordPress will produce the cron, but the cron must hold up for the worker, and so fair sits there.

This is the easiest method of managing WordPress cron jobs, and you can quickly view, edit, create, delete, and can do much more with WordPress cron events.

So let’s get started!

Step 1: Install WP Control Plugin

Just like any other plugin, you need to install WP Crontrol from the plugin repository and activate it.

WP-Control-Plugin

Once it’s installed, you’ll notice that a new option, “Cron Events” has appeared in the Tools tab.

Cron Events

Step 2: View and Understand Cron Events

Next, you need to click Cron Events and see a list of cron events running on your WordPress application.

Cron-Events-List

You can run, edit, and delete your cron events from this section. You’ll see this option when you hover your mouse over any cron event.

You’ll also find useful information about these cron jobs and quickly identify which cron job is used for what purpose and much more.

  • In the first column, Hook shows the name of the hook used by the WordPress core or any plugin. For instance, the prefix “wp” simply shows it’s a cron of WordPress core.
  • You’ll also notice that there’s no option for deleting these cron events however you can use the edit and run now options. Similarly, for your plugins, you’ll notice other prefixes being used (for instance, for WooCommerce, “wc” or “woocommerce”).
  • The second column, Arguments, just contains the arguments passed to the hook functions.
  • The third column shows when this cron job will run next time.
  • The fourth column, Action, shows what action this cron will perform (the “function” of the cron job).
  • The last column defines the recurrence timings of the cron jobs. This is the time interval for the scheduled task.

Step 3: Edit Cron Event

Now let’s edit a WP cron job to test this plugin.

  • Go to a hook’s name and click edit.

Cron-events-edit

  • A new section, “Edit Cron Event” will pop up where you’ll see different fields. In my case, I edited the “wp_privacy_delete_old_export_files” hook.
  • Next, I’m going to change the recurrence of this cron job to ‘Once Hourly (hourly)’.

Edit-Cron-Events

Step 4: Adding a Custom Time Interval

Next, I’ll show you how to add a custom time interval for your cron events.

  • Go to WordPress Settings → Cron Schedules.
  • Now, click on the “Cron Schedule” tab, and a new screen will appear where you’ll see the details of the scheduled time intervals.
  • Fill in the fields Internal Name, Interval Seconds, and Display name, and then click Add Cron Schedule.

Cron-Schedule

  • Note that the input for the interval is in seconds. I will use 600 seconds to set the interval to 10 minutes.
  • You can now edit your cron event and replace it with your custom interval.

Edit-Cron-Events-2

Step 5: Adding a New Cron Event

Now you know how to manage the simple tasks for your cron events but what if you want to add your own cron job for your WordPress application? Don’t worry; it’s so simple, and you must follow the instructions below.

  • First, click Add Cron Event, and you’ll get into the add cron section.
  • Then, just give a hook name as you wish (should follow normal PHP naming convention and no spaces).
  • Then, add arguments (it’s optional); in my case, it’s blank.
  • Next, select any option, and put a time in the Next Run field.
  • Last, choose a recurrence time (schedule time) for your new cron and click Add Event.

Add-Cron-Events

  • If you have any queries regarding this, you can check any running cron job settings from your cron events.
  • After adding a new cron job then, go to the cron events tab and check your newly created cron. You’ll notice that the action column will show you None (error). This is because you haven’t defined any actions for this cron job.
  • Therefore you need to write a few lines of code in the functions.php file of the theme and add an action.

Cron Events List 2

Note: Before going any further, it’s highly recommended to back up the WordPress site.

  • Go to WordPress Dashboard → Appearance, and click on the Theme File Editor.
  • Click on the function.php file from the Theme Files option on the right side of the page.
  • Add the following lines of code right after <?php into your theme’s function.php file
add_action( 'cloudways_new_cron', 'cw_function' );
function cw_function() {
wp_mail( '[email protected]', 'Cloudways Cron', 'Cloudways - a Managed Cloud Hosting!' );
}

Note: Don’t forget to replace my email address with your own.

Adding-code-in-fnction-php-file

You can see in the above image I directly edited it with the WordPress dashboard. If you wish, you can edit this file via an FTP client like FileZilla.

Next, check the email inbox and see whether we’re receiving the message.

Gmail cron job email

Tada! It’s done, and we’ve successfully added a new cron event.

How to Manage WordPress Cron Jobs With WP CLI

You can also view and manage all your WordPress cron jobs from WordPress CLI. At Cloudways, you must launch the terminal and log in with the server credentials.

  • You can use PuTTy to log in to your server. Enter the IP address of your server, and click Open.

PuTTY-login

  • Enter the username and password of your server in PuTTy from the Cloudways server management panel.

Server-master-credentials

  • Next, place the path of your WordPress application. In my case, the URL was :
cd applications/urrghmpqks/public_html/

WP-CLI-Change-Directory-command

  • Next, you need to run this command:
wp cron event list

wp-cron-event-list

  • You can see the hook name, next run time, next run relative, and recurrence (scheduled time).

How to Set Up a Real Cron Job

Now, you know what WordPress cron job is and how it works. What if you want to replace it with a real cron job for low traffic, important tasks that need to be run at a particular time, excessive DDoS attacks, or high page load time?

So let’s get started!

Step 1: Open and Edit wp-config.php File

First, you need to open the wp-config file using an FTP client like FileZilla or SSH client such as PuTTY. After that, edit the file and place the following line of code before the line where it says /* That’s all. Stop editing! Happy blogging. */

define('DISABLE_WP_CRON', true);

WP Config file cron job

This code disables the running cron events on your WordPress site, and now you can create a real cron job.

Step 2: Adding a New Cron Job to Server

Next, log in to your Cloudways platform, then go to Application Management panel → Cron Job Management → Advanced section.

Then add the following command and click Save Changes:

*/5 * * * * wget -q -O - 'https://wordpress-413270-1299955.cloudwaysapps.com/wp-cron.php?doing_wp_cron' >/dev/null 2>&1

Cron Job management

In the above command, first, I defined an interval of five minutes for the cron job. You can use this site to check the time schedule expression. In my case, */5 * * * *is the interval expression.

That’s all! You’ve successfully added a real cron job.

Summary

I hope this article helped you understand how the WordPress cron job works. I’ve discussed two methods to manage WordPress cron jobs; one through a plugin and the other through WP-CLI. I’ve also discussed setting up a real cron job on a WordPress live server. Please let me know in the comment section below if you have any questions.

Frequently Asked Questions

Q: How do I run a cron job in WordPress?

A: There are three ways to run the cron jobs in WordPress.

  1. First, you can use the WP Control plugin.
  2. Second, you can use WP CLI commands.
  3. Third, you can run cron jobs from the Cloudways Platform (if you are a Cloudways customer).

Q: Where are WordPress cron jobs stored?

A: Cron jobs are stored in the wp_options table of the WordPress database where the option_name is cron. You can run the following SQL command to see the cron data.

select * from wp_options where option_name = 'cron'

Q: What is WP Cron Control?

A: WP Cron control is a WordPress plugin that lets you view and control what’s happening in the WP-Cron system.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Customer Review at

“Beautifully optimized hosting for WordPress and Magento”

Arda Burak [Agency Owner]

Farhan Ayub

Farhan is a community manager at Cloudways. He loves to work with WordPress and has a passion for web development. Mostly, he spends his time interacting with the people in the WordPress community. Apart from his work life, Farhan spends his time gaming and playing sports. Feel free to contact him at [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!