WordPress is a powerful platform, but like any software, it requires regular maintenance to perform optimally. One such maintenance task involves cron jobs, which are scheduled tasks that WordPress uses to perform various functions. However, the default WordPress cron handler, WP-Cron, can sometimes slow down your site. This guide will show you how to disable WP-Cron and use a system cron job instead for improved performance.
Understanding Cron Jobs in WordPress
Cron jobs are essential to WordPress, allowing tasks to be scheduled at specific times, dates, or intervals. These tasks can include publishing a scheduled post, checking for updates, or activating a backup plugin on a set schedule.
WordPress uses a built-in cron handler, WP-Cron, to simulate system cron functionality. However, WP-Cron can potentially slow down your site, especially if it receives high traffic.
The Performance Impact of WP-Cron
WP-Cron isn't a true cron job; it's a workaround created by WordPress to emulate what a system cron does. WP-Cron doesn't run continuously. Instead, it's triggered on every page load, which can cause problems for high-traffic sites. If a site lacks sufficient PHP workers, a request might come in, WordPress will initiate the cron, but the cron has to wait for the worker, resulting in delays.
For low-traffic sites, schedules could be missed because no one has loaded a page. A more efficient approach is to disable WP-Cron and use the system cron instead. This method operates on a pre-defined schedule and is even recommended in the official Plugin handbook.
To disable WP-Cron, insert the following code into your wp-config.php file, just before the line that says "That's all, stop editing! Happy blogging." This action prevents WP-Cron from running on page load, but not when you call it directly via wp-cron.php.
Scheduling System Cron
To schedule a system cron job, follow these steps:
- Log in to your cPanel account and navigate to the 'Cron Jobs' option in the Advanced section.
- In the 'Add New Cron Job' section, select a schedule. Your hosting provider likely has a limit on the frequency of cron jobs. Twice per hour is a common setting for shared hosts.
- Enter the following command, replacing https://domain.com with your domain name. This command might vary slightly based on your hosting configuration. Then click on "Add New Cron Job."
wget -q -O - https://domain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
Note: The >/dev/null 2>&1 part of the command suppresses email notifications.
By following these steps, you can optimize your WordPress site's performance by replacing WP-Cron with a system cron job. Remember, regular maintenance is key to keeping your WordPress site running smoothly.