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.

How to Manage WordPress Cron Jobs

September 22, 2021

7 Min Read
wordpress cron

Many WordPress developers, especially beginners, have a common question “what is WordPress cron” with “how to manage WordPress cron jobs?” a close second.

The answer is simple: use any of the WordPress cron jobs management methods that I will outline in this article.

 

Cron is a standard UNIX utility for scheduling task execution (script or command) at a specific time, date, or interval. The task that it’s going to 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, and that’s why before diving into action and process, you need to understand what a WordPress cron is and how it works.

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 are dependent on users visiting your site, which means 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.

In this article, I’m going to show you how to manage WordPress cron jobs and set up a real cron job for your WordPress site.

How to Manage a 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 Plugin

I’ll use WP Crontrol plugin that allows you to view and control WordPress cron for your WordPress application. It is the popular plugin used to handle WP cron jobs and has more than 100,000 active installations.
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. 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 you’ll see a list of cron events that are running on your WordPress application.

list of cron events

From this section, you can run, edit, and delete your cron events. 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 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 contain the arguments that are passed to the hook functions.

The third column shows when this cron job is going to run next time.

The fourth column Action shows what action this cron is going to perform (the “function” of the cron job).
The last column defines the recurrence timings of the cron jobs. This is basically 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. A new section “Edit Cron Event” will popup 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 “Every Fifteen Minutes”.

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. For this, click on the “Cron Schedule” tab and a new screen will appear where you’ll see the details of the scheduled time intervals. Next, scroll down to the bottom where you’ll see “Add Cron Schedule”. Fill the fields and click Add Cron Schedule.

add cron schedule

Note that the input for the interval is in seconds. I will use 600 seconds to set the interval to 10 minutes.

Now you can edit your cron event and replace it with your own custom interval.

edit cron event

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 all you need to do is 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) and in my case it’s blank. And 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 event

If you have any queries regarding this then you can check settings of any running cron job from your cron events.

After adding a new cron job then go to cron events tab and check your newly created cron. You’ll notice that in the action column it 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.

none error

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

Let’s open the file and add the following lines of code right after <?php:

add_action( 'cloudways_new_cron', 'cw_function' );
function cw_function() {
wp_mail( '[email protected]', 'Cloudways Cron', 'Cloudways - a Managed Cloud Hosting!' );
}

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

function php email address
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, let’s check the email inbox and see whether we’re receiving the message.

cron 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 need to launch the terminal and login with the server credentials.

Next, place the path of your WordPress application. In my case, the URL was :

cd applications/urrghmpqks/public_html/

cron job WP CLI

Next, you need to run this command:

wp cron event list

cron job WP CLI

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 due to any reason like 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. */

Code:

define('DISABLE_WP_CRON', true);

edit wp config file

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

Manage Real Cron Jobs Through Cloudways Platform

Start With 3 Days Free Trial

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.

adding a new cron job

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

advanced cron tab

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.

Congratulations! You’ve successfully added a real cron job.

Final Words!

I hope this article helped you understand how 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 how to set up a real cron job on a live server.

If you have any questions, please let me know in the comment section below.

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!