Optimizing WordPress Cron (WP-Cron) for Better Performance
WordPress relies on scheduled tasks to run various background operations, such as publishing scheduled posts, checking for updates, generating backups, and sending newsletters. These tasks are managed by a built-in feature called WP-Cron.
What is WP-Cron and why optimize it?
To make WordPress easy to install for everyone, WP-Cron is designed to execute whenever a visitor accesses your website or when you perform actions in your WordPress dashboard. While convenient, this method has several drawbacks that can impact your server’s performance and the reliability of scheduled tasks:
- Increased Server Load: Every page load triggers a check for pending WP-Cron tasks, which can significantly increase server load, especially on sites with high traffic.
- Erratic Execution: On low-traffic sites, WP-Cron tasks might not run frequently enough, causing delays. Conversely, on high-traffic sites, it can trigger too often, potentially leading to resource spikes or even issues like duplicate emails.
- Inconsistent Scheduling: If you use caching plugins (which are highly recommended), visitors might primarily see cached pages, further reducing the frequency of WP-Cron checks and making scheduled tasks unreliable.
For these reasons, it’s highly recommended to disable WordPress’s default WP-Cron behavior and instead set up a “real” system cron job. This ensures tasks run at precise, predictable intervals, reducing server load and improving reliability.
Disabling WP-Cron
The first step is to prevent WordPress from executing wp-cron.php on every page load.
- Access your wp-config.php file: This file is located in the root directory of your WordPress installation. You can access it via SFTP/SSH or your hosting control panel’s file manager.
- Add the following line: Place the line below just before the
/* That's all, stop editing! Happy blogging. */line in yourwp-config.phpfile.
/* Disable background wp-cron */
define('DISABLE_WP_CRON', true);Save the wp-config.php file after adding this line.
Setting up a system cron job
After disabling WP-Cron within WordPress, you need to create a system cron job to trigger wp-cron.php at regular intervals. This ensures your scheduled tasks continue to run.
Common settings for cron jobs
The frequency of your cron job depends on your site’s needs:
- Twice a day: Sufficient for most blogs that don’t rely heavily on exact scheduled post times.
- Once an hour: A good balance for moderate usage.
- Every minute (
* * * * *): Recommended if you use scheduled posts heavily, as it ensures posts publish at their exact scheduled time (e.g., an article scheduled for 3:30 PM will post promptly).
Example cron command (Standard method)
Here’s the most common command to add to your cron job. Replace username with your actual cPanel/server username and /home/username/public_html/ with the correct path to your WordPress installation’s root directory.
0 0,12 * * * /usr/bin/php -f /home/username/public_html/wp-cron.php >/dev/nullExplanation:
0 0,12 * * *: This is the cron schedule. In this example, it means “at minute 0 past hour 0 and 12” (i.e., at 12:00 AM and 12:00 PM every day). Adjust this based on your desired frequency (eg:* * * * *for every minute)./usr/bin/php -f: Specifies the PHP interpreter to run the file./home/username/public_html/wp-cron.phpThe full path to your WordPresswp-cron.phpfile. Adjust it as per the scenario.>/dev/null: This redirects standard output to null, preventing cron from sending you emails for every successful execution.
Important: Initially, you might want to omit >/dev/null to see the output and confirm the cron job is running correctly. Do not add 2>&1 as this redirects error output to null, which you generally want to see if issues arise.
Conclusion
Disabling the default WP-Cron and implementing a proper system cron job is a highly recommended optimization for any WordPress site. This simple change can significantly improve your server’s performance, reduce unnecessary resource consumption, and ensure your site’s scheduled tasks run reliably. If you encounter any difficulties or need assistance, Liquid Web’s Heroic Support® team is always available to help.