WordPress Cron jobs solved

WordPress has an interesting mechanism referred to as “cron”, but it’s not cron as you may know it, if you’re of a more technical leaning.

There are a number of background tasks that need to run in order to keep a WordPress site ticking along nicely.  For example, scheduled posts.  Ever wondered how these actually get published, as if by magic?

Well the way WordPress handles this is that whenever someone accesses your website, it checks to see if there are any jobs that are due to be run (such as publishing a scheduled post) and runs them before it shows the page contents.  This is done asynchronously, which means it doesn’t actually slow down the display of your page, in theory.  But I’ve seen sites where this isn’t working properly and can cause really sporadic delays, caused by these “cron” jobs running.

In this case, there is an alternative method that can be enabled.  This works by first displaying a page which triggers the jobs running but then redirects the user to page of your website they wanted – this is supposed to make the jobs run properly asynchronously, but sounds like a bad user experience to me, so I’ve never tried it.

The other problem with these approaches is that I use Cloudflare page caching (check out my course on setting up Cloudflare and WordPress for how to do this) – this means that Cloudflare returns my cached page content from it’s edge nodes and therefore my server is never hit, which means my “cron” jobs never run!

So what’s the solution to that?  Well the top suggestion is to setup a proper cron job on the server – this is a scheduled task on the server which triggers the WordPress “cron” process at a regular interval, such as every 15 minutes.  Unfortunately this option isn’t available to me either, as I have cheapy shared hosting at the moment and they don’t allow access to create cron jobs.

Is all hope lost?  No!  And here’s what you do…

Firstly, you need to disable the WordPress “cron” processes, so that they are not kicking in.  This will improve performance and give you control over when they run.  To do this, add the following lines to your “wp-config.php” files…

define('DISABLE_WP_CRON', true);
define('ALTERNATE_WP_CRON', false);

The second isn’t necessarily required, as it’s disabled by default, but just make sure that you don’t have it enabled in your file, either by setting it to false or removing the line completely.

Now sign up for an uptime monitoring service, which is a really good idea anyway.  Personally I went with Uptime Robot, as their free plan lets you add 50 monitors, which is plenty for my needs.  You can also setup notifications to arrive in a number of different ways – personally I prefer email and/or Slack for mine.

On Uptime Robot you can setup monitors in many ways, but for this purpose you want an “HTTP(S)” monitor and the URL should be something like…

https://bettersecurity.co/wp-cron.php?doing_wp_cron

Obviously you need to use your own domain and path to your WordPress directory(root folder for me) but the rest of the URL is very important for this to work.

I’ve set my monitor to run every 5 minutes, which is the most often for the free plan, but to be honest every 15 minutes should be more than sufficient.

One more step for me, because as I’ve said, I’m using Cloudflare caching quite extensively, was to add a Page Rule that disables the caching for this URL (check out my course on setting up Cloudflare – Page Rules deep dive for more help on this).

And now I can see my “cron” jobs firing on a regular basis, scheduled posts published really promptly (within 5 minutes of the publish time), and my website running smoothly.  Not bad for a few minutes work.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.