Optimize WordPress: replace wp_cron with a real CRON job

WordPress comes with a built in function for automation called wp_cron. It is used for various WP maintenance (updates check etc.), to publish scheduled posts that are created with a date in the future, and by many plugins with automated features.

Though wp_cron is not perfect. You might have seen missed schedules already if you use such functions.

This is because wp_cron is not actually a cron job, but rather a “pseudo cron job” that fires when a page is loaded (i.e., when you get a visitor to your site.)
When someone visits you site, wp_cron is fired during page load, and checks the database to see if there is any pending action to take.

This can cause two major issues:
– If you don’t get visits during a given period, any scheduled actions will not be triggered.
– Loading wp_cron during every page load negatively affects your site speed.

Here’s a simple tweak to replace wp_cron with a real CRON job to solve these issues.

Disable wp_cron

1. Using your FTP program (or file manager in cPanel), download the file called wp-config.php which is located at the root of your WP installation.

2. Open wp-config.php with a text editor and paste the following line.

define('DISABLE_WP_CRON', true);

3. Save and upload wp-config.php back to your server.

wp_cron is now disabled. But don’t stop here! You need to replace it with a real CRON job, otherwise you will loose the benefit of every WordPress automated features.

Setting a real CRON job to replace wp_cron

Option #1: set a CRON job through your hosting’s cPanel

4. In your cPanel, go to “cron jobs”.

5. Under the “add new cron job” section, choose a 15 minutes interval (this setting seems to offers the best performance).

6. In the “command” field, paste the following line (just change YOURWEBSITE.COM with the actual URL of your WP site!)

 wget -q -O - http://YOURWEBSITE.COM/wp-cron.php >/dev/null 2>&1

7. Click “Add New Cron Job” and you’re done!

Option #2: Use a third-party CRON job service

Some hosting provider do not offer adequate CRON functionalities. Some can be unreliable, some only allow a limited number of jobs, or do not allow you to use an interval smaller than an arbitrary minimum. And some hosts even simply don’t provide CRON support at all.

In such case, using a reliable third-party CRON job service is the solution. They will basically “ping” any URL you tell them to at a set interval like clockwork.

I recommend easycron.com, they even have a free plan that should be good enough for most small sites or blog. They also have very affordable paid plans (starting under $8/year) should you need more executions per day and longer timeouts.

Setting up a third-party CRON job with easycron.com is as easy as the name suggests:

– Click “Create New Cron Job”

– In “URL to call”, enter http://YOURWEBSITE.COM/wp-cron.php (just change YOURWEBSITE.COM with the actual URL of your WP site!)

– Choose an interval to determine how often the job should run

– Click “Create Cron job” and you are done!

That’s it, you’ve just replaced wp_cron with a real CRON job!
From now every function hooked to wp_cron will run every 15 minutes no matter what.
No more missed schedules, and you have (slightly) optimized your site speed on every page load!

Share the love!