How to Replace WordPress Cron with a Real Cron Job
Have you ever noticed that your scheduled posts in WordPress are missing? Though WordPress has its own cron feature that dictates the scheduling of your blog's posts and events, WP-cron is not a literal cron job.
Linux cron job vs. WP-cron
The user can control Linux cron jobs, ensuring that the items constrained by the schedule are run appropriately. Also, for a high-traffic WordPress site, a Linux cron job can reduce the chances of downtime by lowering the bandwidth on the server, thus using fewer server resources.
WP-cron is a virtual cron that only works when the page is loaded. WP-cron is first loaded by WordPress when a page is requested on the site's front or back end. At this point, WP-cron displays the necessary page to the site visitor. Though convenient, it is known to fail for a variety of reasons, including but not limited to the following:
- Conflict of plugins
- DNS-related issues
- WordPress bugs
- Use of caching plugins
- Large server load
How to replace WP-cron With a Linux cron job
The best way to optimize the efficiency of your WordPress cron jobs is to disable WP-cron and set up a normal cron job through cPanel, which will run every hour.
This video will help walk you through your first time replacing a WP-cron with a Linux cron job.
The buttons, links, and icons in your cPanel may look slightly different from the video; however, their functionalities in cPanel should still be the same.
Step 1: Disable wp-cron.php
You can disable WP-cron by modifying the wp-config.php file, which is located in your WordPress site's document root.
To disable wp-cron.php:
- Open the wp-config.php file.
- Add a new line after <?php.
- Add the following code on the new line:
define('DISABLE_WP_CRON', true);
Step 2: Set up Linux cron
You need to have a working knowledge of Linux commands before you can use cron jobs effectively.
To set up a Linux cron job:
- Log in to your cPanel.
- In the Advanced section, click Cron jobs.
- Under Add New Cron Job, select the time interval. HostGator recommends that you do not set the interval lower than 15 minutes.
- Set the cron command to the following, replacing yourwebsite.com with your actual domain name:
wget -q -O - http://yourwebsite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
The above command tells the Linux server to run wp-cron via wget, which will trigger the wp-cron.php script to do its job on your schedule instead of on each page view. - Click Add New Cron Job to set the cron.
To test out the new cron, wait for the elapsed period for the cron to run. If the cron does not run, please review the steps listed above to ensure that you have completed all steps correctly.
Staggering multiple WordPress cron jobs
Creating cron jobs for multiple WordPress cron without custom configuration can cause them all to fire at the same time. Having multiple cron jobs run simultaneously can cause your CPU usage to increase, negating the benefit of using Linux cron jobs.
To stagger your cron jobs:
- Divide 60 by the number of WordPress installations you have. If you have more than 60, use the number 1. For example: If you have 6 WordPress installations, 60 divided by 6 equals 10.
- When creating your WordPress cron jobs, use 0 for minutes for your first cron job.
- For each cron job after the first one, add the number you got in Step 1.
Note: If you have more than 60 WordPress sites, after you reach 59, you will need to start at 0 again. If you experience CPU issues with this many WordPress sites running cron jobs this frequently, you may wish to consider migrating some of your sites to a second account.
By following these steps, you will ensure that your WordPress cron jobs run once per hour and that there is as little overlap as possible between them, which should greatly improve your CPU usage with this platform.