How to Create and Modify a WordPress Cron Job

wp cron job

Cron is a tool that can help Unix/Linux systems schedule tasks. These tasks can be very useful in saving time on your day-to-day tasks, especially when you have a content-rich environment like WordPress. WordPress has developed WP-Cron specifically to cater to this need. WordPress uses wp-cron.php to handle tasks on responsible servers.

This article will demonstrate a step-by-step process of creating and modifying the cron jobs in WordPress.

What is the Purpose of WordPress Cron Jobs

In WordPress, there is a file called wp-cron.php, which has the WP-Cron Service configured and does some tasks like posting items, checking for the latest plugins and themes, etc. Just as the bank account needs you to log back in after a certain time of inactivity, WordPress also logs you out with wp-cron.php. But with plugins or changes in code, such as rules of WordPress Login, it can be skipped.

The underlying issue of wp-cron is its lack of reliability. Sites with no traffic and some form of caching are not reliable for having cron issues, triggering missed updates, and consuming resources as tasks accumulate. Apart from this, there is a big performance issue with wp-cron, which is that it runs on every page load, and this can cause a big performance hit, especially on larger sites with slowly loading pages.

You have two possibilities available that can schedule the WP-Cron job, but the most recommended one is to disable it in WordPress and then use a real cron job on your server. This strategy ensures that everything occurs when it is supposed to and not on a page visit. The best way to schedule events in your WordPress site is through Cron on your server. If you don’t have the technical knowledge, you can utilize some simple software to edit your WordPress cron jobs.

How WordPress Cron Jobs Benefit Your Website

With Cron jobs in WordPress, your site can do key tasks on time. For example, think of a membership site that sends updates or reminders each week. If the site does not send these emails on time, users might lose interest or miss your news. Cron jobs help here by sending emails as planned.

These tasks help site owners too, making work easy and neat. As a blogger, you might spend a lot of time looking at comments, which can be tiring. Many of them might be spam. Instead of deleting them by hand, you can use a plugin with Cron jobs to clear spam.

Many WordPress plugins need Cron jobs. But plugins that are not managed well can hurt your site, using too many server resources and slowing it down. It’s key to keep your site up to date, so watch your Cron jobs and keep them balanced.

Pros of WordPress Cron Jobs

Let’s take a look at the advantages of cron jobs:

  • Cron jobs streamline ongoing tasks like sending newsletters or filtering spam comments.
  • WP cron jobs boost site efficiency by setting tasks.
  • Cron jobs ease automation, such as comment checks.
  • These jobs cut down workloads a lot.
  • Many plugins need Cron jobs to work right.

Cons of Cron Job WordPress

Let’s explore some disadvantages:

  • Some plugins that use cron jobs may be hard on the server, slowing the site if not well managed.
  • Cron jobs may fail at times or not run as they should. It is true for busy sites or those with cache plugins, causing tasks to be late or missed.
  • Overloading tasks can put strain on the server, hurting site performance.
  • WordPress needs site visits to trigger cron jobs, which is bad for low-traffic sites, leading to delays and missed jobs.

Creating and Modifying a Cron WordPress

WP-Cron works differently from traditional system cron jobs, each having its unique advantages. Using WP-Cron, tasks run on set times, and system cron jobs can be defined to run tasks on the same time interval. It is important to know that WP-Cron checks to see when a visitor has viewed a page; if they have, it starts the processes set to WP-Cron, and if the site has no traffic, WP-Cron does not work.

If you want to make your custom things with WP-Cron, you have to put custom hooks. The WP Crontrol free plugin is also available, which provides easy installation and direct access to WP-Cron tasks from your WordPress dashboard.

Installing WordPress WP Crontrol Plugin

WP-Crontrol is a popular plugin with over 300K active users and a rating of 4.5 out of 5. You can find it directly inside your WordPress dashboard by navigating to Plugins > Add New and then clicking the Install Now button:

install wp crontrol

After you have installed the WP Crontrol plugin, you will be able to view, edit, and delete cron jobs that you have in your WordPress installation. Remember, you need to activate this plugin in order for it to be functional:

activate wp crontrol

Creating a WordPress Cron Event

Once you’ve activated the plugin and configured the “Cron Schedules” section in WordPress settings, hit Add Cron Schedule to introduce the WordPress Cron Job:

add cron schedule

You can also add the custom schedule (if needed) in your functions.php file like this:

add_filter('cron_schedules', 'ultahost_cron_schedule');
function ultahost_cron_schedule($schedules) {
    $schedules['every_five_minutes'] = array(
        'interval' => 300, // 300 seconds = 5 minutes
        'display'  => __('Every 5 Minutes')
    );
    return $schedules;
}

Updating an Existing Cron Event

To modify an existing cron event, go to Tools and then select Cron Events. Now, locate the event that you want to modify and click Edit:

update cron event

Here, you can update the time, next run, etc.

edit cron event

Setting up a Cron Job Through cPanel

In order to set up cron jobs through the control panel, use the cPanel that your web hosting provider gives you. The section is typically found under the Advanced tab, so find Cron Jobs and click on it:

open cron jobs cpanel

In the Cron Jobs section, you’ll see options to set time intervals: minute, hour, day, month, etc.

add new cron job cpanel

The Common Settings menu offers pre-configured options, like running once a minute or once a year. You can pick one of these or fill in the fields below to set your own custom time:

edit common settings

After setting how often the cron job should run, enter the command it needs to perform in the Command field. Then click on Add New Cron Job to save it:

Add New Cron Job cPanel

Once a cron job is successfully created, you will see it listed under Current Cron Jobs at the bottom of the page:

confirmation

You can click on the Edit or Delete button to modify or remove a cron job.

Conclusion

Cron jobs serve as valuable resources in the automation of repetitive activities on your WordPress website. Such tasks thereby saved time and enhanced productivity, further proving their importance. WP-Cron and other related tasks can help automate the weekly task of publishing new content, sending out newsletters, and even moderating spam comments. 

However WP-Cron plugin is good, but a lot of times, this runs even when no request hits the server. So, you can replace the WP-Cron with a real server-based Cron job, which is limited to certain sites. After you have managed the task via a plugin like WP, there are some requirements that you should do manually so that these tasks keep running without any concerns. To easily manage tasks on your site so they run smoothly and without any delays or lapses, we have discussed how to do just that in the above guide.

We hope this article has enabled you to create and modify WordPress cron jobs. Consider Ultahost’s fast, user-friendly, and secure WordPress hosting to improve the security of your website. Ultahost provides features like enhanced protection against unauthorized access and unlimited bandwidth. All these features are optimized for superior website performance.

FAQ

What is a Cron job in WordPress?
How does WP-Cron work?
Can I manage WordPress Cron jobs without coding?
What are the benefits of Cron jobs in WordPress?
How do I set up a Cron job using cPanel?
How can I create custom Cron schedules in WordPress?
Can Cron jobs ever negatively impact the performance of my website?

Related Post

How to block an IP address in cPanel

cPanel is a popular web hosting control panel known for...

How to Set Password Protected WordPress Page

In many cases, you might want to restrict access to cer...

How to Fix “Sorry you are not allowed t

The "Sorry, you are not allowed to upload this file typ...

How to Fix the HTTP Image Upload Error in Wor

Have you ever received the avoided HTTP image upload er...

How to Install Tutor LMS in WordPress

Tutor LMS is a Learning Management System plugin in Wor...

How to Embed PDF in WordPress

PDF is a popular web document format due to its flexibi...

Leave a Comment