Replace WordPress Cron with a Real Cron Job

WordPress comes with WordPress Cron out of the box. This feature allows WordPress to run scheduled tasks without having to configure an actual cron Job in the server.

 

How WordPress Cron works

WordPress Cron works by checking and running scheduled tasks every time someone visits your WordPress website. This is fine for most websites but for some websites, this may not be enough. Here are some issues with the WordPress Cron.

  1. Scheduled tasks may not run at the exact time specified for the task.
  2. Each request to the website checks for pending tasks.

In order to optimize WordPress Cron, we have to disable it in WordPress and replace it with a real cron job.

 

Edit the wp-config.php file

The wp-config.php file contains all the settings related to your WordPress site. This file can be found in the root directory of the WordPress website together with the index.php.

Find the location of the file and edit it using your favorite editor, vim in my case.

cd /var/www/html/
vim wp-config.php

 

Disable WordPress Cron

Enter the following line of code just above the /* That’s all, stop editing! Happy blogging. */ comment.

define('DISABLE_WP_CRON', 'true');

/* That's all, stop editing! Happy blogging. */

 

Edit the Crontab

Crontab is basically a list of commands to be run by the server at the specified time. This file can be edited through the command line or through a visual interface in Control Panels.

Enter the following command to edit the crontab in the command line.

crontab -e

 

Add a Cron Job and Save

A cron job is simply a command to be run at the specified time. The following line means run the curl command every 5 minutes.

Enter the following line of code at the last line of the crontab and save. Do not forget to replace the URL with your WordPress website.

*/5 * * * * curl https://www.your-website.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1

 

That’s it, your WordPress is now checking and running the scheduled tasks at the schedule of the crontab.

Comment

There is no comment on this post. Be the first one.

Leave a comment