Tech Notes: Heart Internet and WordPress wp-cron job scheduling

This was a little fault that I found in my development environment, which I’ve recently moved onto my Heart Internet hosting as explained the other day.

The issue is specific to Heart Internet’s hosting and results in any scheduled jobs not running!  Scheduled jobs could include automatic updating, database cleaning and posting of scheduled posts, but could also include other features provided by plugins, such as automated database backup.

wp-cron – how WordPress does scheduled jobs

WordPress normally runs on a Linux or Unix server, and, as such, you could use the “cron” feature of Linux/Unix to run scheduled jobs. BUT…most web hosts don’t let you have access to cron – and rightly do: you could do a lot of damage through incorrect use.

WordPress, though, needs to be able to do some useful scheduled tasks, things like cleaning up deleted items, and publishing posts scheduled for future posting.

So, WordPress uses what is often called “pseudo-cron”. There is a script – wp-cron.php – that runs all the scheduled jobs, and WordPress tries to run this script whenever someone visits your website.  It does this by loading the web page http://<your-domain>/wp-cron.php  It will do this at most once per minute.

You can use a plugin like cron-view to see what’s in your list of scheduled jobs. For me, it was some automated digest emails not sending that brought the issue to my attention!

A Heart Failure

Heart Internet don’t allow a PHP script (or any script) running on its server to make an HTTP request back to the domain that the script is running on. It’s possible, though I’ve not investigated it, that they don’t allow a request back to the server, or to any of their servers, potentially blocking HTTP access to other domains that they host too.

So, the short story is that if you’re running an out-of-the-box WordPress install on Heart, none of its scheduled jobs will work!

I discovered this thanks to numerous forum posts, including this 6-day-old one on the WordPress support forums for a backup plugin.

A Solution

There’s another very helpful discussion over on the WordPress Forums that both explains why wp-cron is implemented the way it is, and offers a workaround: add the following to your wp-config.php

define('ALTERNATE_WP_CRON', true);

The fix is described as “a bit iffy sometimes”, and I’ve not tested it, but I would recommend giving it a go if you’re running WordPress on Heart’s servers.

Another Solution

Another way to tackle this is to make use of the fact that Heart let you run cron jobs anyway, so you can just set up a cron job to run once every 15 mins to run wp-cron.php.  This is probably a better solution but it’s a bit of a pain setting it up for every WordPress install you’ve got…if, like me, you have many!

Tags: , , , , ,

  • http://twitter.com/tahninial Dan Moat

    I’m glad you posted this it has saved me a lot of head scratching. Just as I discover wp-cron I realise it is redundant haha. I will try the alternate method else, while I can only do hourly cron jobs with my Heart (or 20 minutes if I set up three), it will have to do!

  • http://twitter.com/whiterabbitweb Lucy Rabbit

    Aha! I am seeking a decent backup solution for my many hosted WP client sites on Heart. Would be best if one cron job could rule them all… The alternate cron setting in wp-config works for me but makes an ugly call you can see in the URL. This is likely to upset my clients who aren’t sure what is happening. More if I figure out a more elegant solution for multiple sites…. Thanks for your post btw!

    • http://oikos.org.uk/ Ross Wintle

      Hi Lucy,

      If you have “many hosted WP client sites” I’d recommend using a management tool like InfiniteWP, ManageWP or iControlWP? These handle not just backups but WordPress updates, site migrations/clones, comments, posts, pages, users, and so on. I’ve done a comparison at http://oikos.org.uk/2012/06/wordpress-management-tools-faceoff-managewp-vs-infinitewp-vs-wp-remote/ – it’s a bit old now (I need to add iControlWP) but gives you an idea of what the tools look like and do.

      Drop me a Tweet if you want a hand (I’m better on @magicroundabout than @oikos_uk)

      Thanks

      Ross