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
Linux cron jobs can be controlled by the user, making certain that the items constrained by 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 front or backend of a site, at which 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. If you have multiple WordPress sites running on your server, be sure to check below for how to ensure that your WordPress cron jobs do not all run at the same time.
1. Disable wp-cron.php
You can disable WP-cron by modifying the wp-config.php (located in the folder where WordPress is installed). Open the wp-config.php file, add a new line after <?php, then add the following code on the new line:
2. Set Up a Linux Cron
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>&1The 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 time 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 crons 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.
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.