Have you ever wondered how you can run scheduled tasks on your WordPress website? Well, adding cron jobs in WordPress is the solution, and here’s how to do it!
As a WordPress user, there are instances or situations when you want to run certain tasks on your website so that these tasks can be done automatically at given intervals. Here’s a perfect example. Let’s say want your server to automatically back up certain files after every two hours, adding a cron to your server will solve this. While WordPress is designed with its in-built cron system known as WP-Cron, it’s not very reliable, so you might want to add Cron Job plugins to help with these tasks.
Cron is a system technology that runs scheduled tasks known as Cron Jobs on websites. Of course, WP has a cron system known as WP-Cron, which performs scheduled tasks such as publishing scheduled posts, checking for updates, and many other repetitive tasks. But because WP-Cron isn’t very reliable, you should know how to schedule Cron Jobs in WordPress to help make your certain administrative tasks a lot easier.
This guide, therefore, covers everything that you need to know as far as adding, setting up, and managing Cron Jobs in WordPress are concerned.
What is WordPress Cron and How Does it Work?
Cron is technically a UNIX system that most servers use to command and run scheduled tasks at certain intervals either at periodic fixed times or dates. In most cases, these tasks are repetitive, so you can use a cron system to automate them and save time. These tasks generally include publishing scheduled posts, checking for updates, deleting old comments from trash, and many other repetitive tasks.
Like most servers, WordPress has its cron system known as WP-Cron. As a WordPress website owner, you benefit every day from the WP-Cron, whether knowingly or unknowingly. Without it, you’d be stuck with the challenging task of having to manually the backup button each time you want to back something on your website.
In its default configuration, WordPress doesn’t use its WP-Cron to schedule updates and other tasks. Instead, it’s designed in such a way that WP-Cron will only run and check for scheduled tasks when someone visits your website. This means that the scheduled tasks will only be run if their scheduled time has been met.
What’s the Difference Between WP-Cron and Other Crons?
Although they use almost the same system, the main difference between WP-Cron and other crons is in functionality. WP-Cron is designed to work only when a user visits your site. This is generally very unreliable in the sense that important updates or schedules might not be run if users do not visit your website.
On the other hand, crons are designed to run some tasks not based on users but on the time that these tasks are scheduled to run or occur. This is one of the main reasons why we prefer external cron job plugins to the in-built WP-Cron.
Why Do You Need to Add Cron Jobs in WordPress?
There’s no doubt that WP-Cron is easy to set up as you do not require any knowledge of the UNIX cron job. However, the problem with this WP-Cron configuration is that it’s primarily dependent on users visiting your website. Without that, these tasks and updates will probably not be executed. In short, WP-Cron is only executed when a user loads up your website. As you can see, this is very unreliable and that’s why many WordPress users prefer server-level Cron Jobs.
With that in mind, it’s always a good idea to disable the internal WP-Cron system and add a Cron plugin from an external service, which is more reliable. Most WordPress Cron plugins are designed with features that simplify the setup and management of their tasks even for beginners. This is of great importance in helping you save time while focusing on other essential tasks by not wasting your time on repetitive tasks that can be scheduled to run automatically.
How to Deactivate WP-Cron for Faster Performance
As we’ve noted severally in this guide, cron jobs are essential in scheduling periodic tasks to be done automatically without your input. But because the in-built WP-Cron isn’t reliable you may want to disable it so that the external cron plugin can work perfectly. All you have to do is add define (‘DISABLE_WP_CRON’, true); to your wp-config.php and you’ll disable the WP-Cron from running.
How to Add Cron Jobs Plugin in WordPress
Having briefly looked at the importance of adding external cron jobs over the in-built WP-Cron, it’s now essential to learn how to add cron jobs plugins to WordPress. While there are several ways of setting up and managing cron jobs in WordPress, we’ll take a look at the most popular method.
Step One: Install the WP Control Plugin
In this guide, we’ll use WP Control as it is free and has some of the best features that you require for this task. With more than 100,000 active installations, WP Control is one of the most popular plugins for WordPress cron jobs. However, keep in mind that you can use any other cron plugin as you deem fit.
That being said, the first step is to install and activate the plugin in WordPress. This is important in allowing you to manage cron jobs effectively. Here’s how to go about it.
- Go to your dashboard and navigate to plugins.
- Search for WP Control and click “Install Now”
- Give it a few minutes to install and selects “Activate” if the installation is successful.
- You can now go to Tools and then Cron Events to view all the cron jobs that are running.
Essential Tip: You should NOTE that it’s vital that you DO NOT delete any default cron job in WordPress. This is because these cron jobs have WordPress prefixes, so deleting them may negatively affect your website’s core features and functionalities.
Step Two: Adding a New Cron Event
The step above allows you to control and manage all your cron jobs in WordPress. Here’s what you need to do to add a new cron job or event.
- Go to the Cron Events sub-menu and click ADD NEW just next to Cron Events.
- You can then scroll down to choose the standard cron event where you can create a hook for any new cron event you want to run. It’s important to ensure that the hook name follows the right naming convention per the PHP standards.
- You can then schedule the intervals or time that you want these events or tasks to occur and their recurrences.
- You can then click Add Event and the new task will appear in the Cron Events if the process is successful.
- With the hook in place, you can then include it in the tasks that run when the cron event is scheduled. All you have to do is access hPanel dashboard and open File Manager under the Files section.
- Go to the root directory and find your theme’s functions.php file. Open it and add a code snippet offered by the cron plugin.
- Save all the changes and load your site. You’ll receive an email if the cron event is successfully added.
Step Three: Setting Cron Scheduling
Most cron plugins offer four standard time intervals to choose from. You also get the option of creating custom time intervals. Here’s what you should do.
- Go to Admin dashboard, setting, and then Cron schedules.
- Fill all the sections under the Add Cron Schedules. Set the right interval for your tasks. You can then use the standards stipulated by PHP to create a name for the interval.
- You can then click Add Cron Schedule and your selected time interval will appear on the list.
Step Four: Editing Cron Events
You can as well edit any existing cron event. Here’s how to do it.
- Go to your Admin dashboard, tools, and then Cron Events.
- Locate any cron event that you had created earlier.
- Go to the Recurrence menu and select the task that you had scheduled
- You can finally click Update Event.
How to Set Up an Actual Cron Job
Situations such as low traffic, excessive DDoS attacks, high page load time, and running vital tasks are among some of the reasons why you might want to set up an actual cron job. Here’s how to do it.
- Access the admin’s cPanel, go to Cron Jobs, and then the Advanced section.
- Add this code (wget -O /dev/null -o /dev/null https://yoursite.com/wp-cron.php?doing_wp_cron) to the Command to run section. Make sure that you change the placeholder with your site’s real URL.
- You can then select the appropriate interval run time as provided on the cPanel.
- Click save to initiate the new cron job settings.
To this end, I hope that this guide has shown you how cron jobs work in WordPress. We’ve gone through the steps that you need to follow when adding a cron job plugin to WordPress if you do not want to rely on the not-so-reliable in-built WP-Cron.
That being said, let’s know what you think about the steps.