Have you ever scheduled a post or a newsletter in WordPress only to see nothing happened and the schedule was missed? Here’s why and how to solve this for good!
WordPress comes with a built in function for automation called wp_cron. It is used for maintenance, automatic update checks, to publish scheduled posts, and by many plugins with various automated features. But it is not perfect.
This is because wp_cron is not actually a Cron job, but a “pseudo Cron job” that fires when a page is loaded. So when someone visits you site, wp_cron is fired during page load, and checks the database to see if there is any scheduled action to take.
It does the job, but has two major downsides:
– 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 (and if there is a scheduled action to take, an unlucky visitor may have to wait before the page actually shows).
Here’s a simple tweak to replace wp_cron with a real Cron job and solve these issues.
Setting a real Cron job to replace wp_cron
1. Login to your host’s cPanel and go to “cron jobs”.
2. Under the “add new cron job” section, choose a 15 minutes interval (you may choose any other interval, but this is a quite common setting and will do in most cases).
3. 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
4. Click “Add New Cron Job” and you are done with this part!
This Cron job will trigger any action that is pending/scheduled in your WordPress every 15 minutes, regardless of your website traffic and without interfering with pageload time.
Now the internal, automatic triggering of wp_cron within WordPress is not needed anymore. It is even unwanted because all it does now is hanging there each time someone visits your sites. This doesn’t make a huge difference on pageload, but still, better get rid of it!
(Warning: you should only do this if you have set up a Cron job, otherwise anything hooked to wp_cron won’t be triggered at all!)
5. 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.
6. Open wp-config.php with a text editor and paste the following line.
7. Save and upload wp-config.php back to your server.
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 enhanced your site speed on every page load at the same time!