WordPress

How To Optimize WP Cron – Using Real Cron Jobs

WordPress comes with a default WP Cron that is a pseudo Cron Job in WordPress that is meant to perform automatic tasks such as scheduling of posts, and various jobs for plugins and themes. The downside is that because its done at an application level the WP Cron only runs if there is a user that visits the website.

Say you schedule a post for 4:00PM and nobody visits your website during that time, then the scheduled post will fail. The reason for this is because the WP Cron can only run when a user visits the site. While this is handy for other tasks its actually a major performance issue. As your site gets larger the WP Cron will continue to run on every single page load and can lead to CPU issues.

To optimize the WP Cron the best thing you can do is actually replace the WP Cron with a real server Cron Job and this can be done easily if you are using CPanel. Your first step will be to properly disable the default WP Cron and then of course enabling the real Cron Job on the server.

To disable WP Cron add the following line to your wp-config file.

/* Optimize Use of WP Cron */
define('DISABLE_WP_CRON', true);

This will completlely disable the use of WP Cron and then the next step is to setup a Cron Job that will run the scheduled tasks in WP Cron on a reasonable basis. I recommend setting this on an interval of once per hour but you can change it.

In CPanel scroll down to where you see the “Advanced Section” and you will see a Cron Jobs button with the picture of a calendar. Click it and you will enter a menu that looks fairly complicated. Don’t worry you will simply need to go down and enter the following code.

wget -q -O - https://ww.sertmedia.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

And then in your time above set it to the following.

Minute: 0 Hour: * Day: * Month: * Weekday: *

Now your WP Cron will be set to run every hour as opposed to on every page load. The best part is if your site was having large CPU Spikes you will notice a nice steady decrease with the introduction of this tactic. Also please note that you do need to change the URL of the website in the Cron Job to have any effect.

Tags
Show More

Scott Hartley

Scott Hartley is the developer and owner of The Daily Exposition who is also a tech enthusiast and writes the majority of content found on the website. When he isn't keeping up to date with the latest and breaking technology news, he is working on his own WordPress plugins, themes, or out exploring the world.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close