Disabling WP Cron in WordPress and Setting Effective Cron Tasks

What is WP Cron and why disable it?

WP Cron is the task scheduling system used by WordPress to run event schedules. This system allows you to perform automatic actions such as scheduled post publishing, deleting spam comments or sending emails at specific intervals. However, WP Cron is based on visits to the siteThis can cause delays in the execution of tasks if there is no traffic on the site.

For this reason, some site administrators prefer to disable WP Cron and opt for a real cron system at the server level. This can help improve the effectiveness of scheduled tasks and ensure that they are executed at the right time.

Disabling WP Cron in WordPress

To disable WP Cron in your WordPress installation, you must make a simple change in the file wp-config.php. This file is located in the root of your WordPress installation.

Open the file wp-config.php and add the following line before the line that says /* That's all, stop editing! Happy blogging. */:

define('DISABLE_WP_CRON', true);

This change will disable the automatic execution of WP Cron and allow you to set up cron jobs manually through your server.

Setting up effective Cron tasks

Once you have disabled WP Cron, the next step is to set up effective cron jobs on your server. This is done through your hosting control panel, which usually includes an option to manage cron jobs.

Disabling WP Cron in WordPress and Setting Effective Cron Tasks
Download our web maintenance guide Free of charge!
Free guide for freelancers and small businesses that want to avoid surprises and improve their web performance.

You set up cron jobs by specifying when you want a command to run. Here are some common examples of tasks you can schedule:

  • Performing backups: You can schedule a task that backs up your database or files at regular intervals.
  • Update plugins and themes: Set up a task that automatically checks and updates plugins and themes in your installation.
  • Delete spam comments: Schedule a task to run processes to delete spam on a regular basis.

Example of a Cron task configuration

Suppose you want to back up your database every day at 2:00 AM. The command to do this could be:

mysqldump -u user -p password database_name > /path/to/your/copy/backup.sql

In your hosting control panel, you would set up a new cron job as follows:

  • Frequency: Daily
  • Time: 2:00 AM
  • Command: mysqldump -u user -p password database_name > /path/to/your/copy/backup.sql

Benefits of using real cron instead of WP Cron

Using a real cron instead of WP Cron has several significant benefits:

  • Reliability: Tasks are executed at the scheduled time, regardless of site traffic.
  • Less load on the server: By disabling WP Cron, you reduce the additional load that can come with having an internal cron running with each visit.
  • Flexibility: You have more control over the scheduling and execution of tasks, allowing you to optimize your site's performance.

Final considerations when using cron

Before setting up cron tasks, it is essential to consider some aspects:

  • Safety: Make sure that the commands you configure are secure and do not expose your server to unnecessary risks.
  • Resources: Some tasks may consume a lot of resources, so it is advisable to schedule them at off-peak times.
  • Tests: Perform tests to ensure that your cron jobs run as expected and do not cause problems on your site.

Common errors when setting up cron jobs

There are several mistakes that users often make when setting up cron jobs. Some of the most common ones are:

  • Forget the use of absolute routes: Always use absolute paths to avoid localization problems.
  • Incorrect configuration of permissions: Make sure that the file permissions are correct to allow script execution.
  • Do not check operation: It is essential to review the logs and check that the tasks are executed correctly.