Making WP-Cron More Efficient

October 12, 2015
Posted in: Code Snippets, How To, WordPress

WordPress takes care of quite a bit for us – automatically. It checks for updates, sends notifications to other blogs, and even publishes scheduled posts while we’re asleep.

By default, WordPress takes care of this by running wp-cron.php, a file in the root of your website. Every time a user visits a page, wp-cron is called, and a new PHP process is created. The function checks whether it’s time to run any scheduled tasks, and initiates anything that’s due.

wp-cron.php usually doesn’t take very long to run, but if you have a very busy site, it may run so often that it starts slowing down, and may begin affecting your site’s performance.

Conversely, you may also run into problems if your site isn’t very busy. For example, if you schedule a post for publishing at a time when users aren’t visiting your site, the function won’t be called, and the post simply won’t go live.

A good way to ensure that WordPress functions don’t get missed, and don’t get called too often, is to disable the trigger on page load, and schedule a CRON using your server’s built-in CRON functions.

Do disable automatic wp-cron triggering, add this line to your wp-config.php file:

define('DISABLE_WP_CRON', true);

Doing this alone means that WordPress won’t do anything automatically, so we’ll need to call wp-cron on our own schedule. Every 5 minutes should be more than enough to ensure that your posts get published right on time, and that you’re not overloading your server. If your posts don’t need to be up within 5 minutes of your scheduled launch, you can run the CRON less often – every 20 or 30 minutes, perhaps.

Your server may have a visual interface for adding CRON jobs – if it does, you can set it to your desired schedule, and have it run this command:

php /home/USER_NAME/public_html/wp-cron.php

You’ll need to replace /home/USER_NAME/public_html/ with your website’s root-relative server path. If you don’t know it, you can get it by creating a php file in the root of your website, and including this code:

echo __FILE__;

You can usually configure an email notification from your server when a CRON runs – this will tell you that it was called, but not necessarily that everything worked. To test within WordPress, you can either wait for updates to come in, or install a plugin like WP-Cron Control.

Scott Buckingham

President / Owner
613-801-1350 x101
[email protected]
Scott is a WordPress expert who has worked on hundreds of web design and development projects. He excels at finding creative ways to solve technical problems. View full profile