Moving WordPress’ wp-cron to crontab

When I first started using WordPress I found it quite slow and I started to look into how to speed it up. What I found is that WordPress does a lot of house keeping every time a page is displayed. One of these house keeping processes it to check to see if it needs to run any scheduled tasks by calling the wp-cron.php file. This slows down your website as it is called every time someone goes to a page weather you have a scheduled task or not and it’s not even a good scheduler because if your site is not very busy, it may not even get called for a while.

WordPress does require scheduled tasks so wp-cron is needed. We are not going to disable it completely, just make sure it gets called at a regular basis (improve the timing) and not at every page view (improve the speed). We will move this functionality over to the servers crontab system. This does mean that you need admin access to the system that is hosting your WordPress. If you are hosting it on a cloud provider, then you may not be able to make this change.

Disable wp-cron from running at each page view.

You need to disable WordPress’ default behaviour of calling wp-cron on each page view. In your WordPress website, edit the file wp-config.php and add in the following line:

define('DISABLE_WP_CRON', true);

Add a crontab on your server

I use FreeBSD to host my WordPress instance. Open a shell terminal and edit the file /etc/crontab. Add the following line to the end:

*/10 * * * * /usr/bin/fetch -q https://example.com/wp-cron.php?doing_wp_cron -o - > /dev/null 2>&1

Make sure to change example.com to the URL of your website. The above line we automatically call the wp-cron function every 10 minutes. You can change the */10 to */5 to make it run every 5 minutes if needed. This will also get run even if you website get little to know traffic making the scheduler more accurate.

This change is highly recommended for any site (if you have access to the servers crontab) but it critical if you have a high volume site. This is probably one of the biggest changes you can do to speed up page loading for your visitors.

Leave a Comment

Your email address will not be published.