Looks like WordPress unnecessarily fire WP CRON on every page load. I'm thinking, instead of having it run on every visit, why not just schedule it to run every 5 minutes via server? I could simply trigger wp-cron.php every five minutes and achieve desired result?

Is there any downside to this?


There is no downside for running WP CRON using the server's cron jobs. In fact this is the recommended practice.

According to Official WordPress Plugin Development Document:

WP-Cron does not run continuously, which can be an issue if there are critical tasks that must run on time. There is an easy solution for this. Simply set up your system’s task scheduler to run on the intervals you desire (or at the specific time needed).

To do this, you need to first disable the default cron behaviour in wp-config.php:

define('DISABLE_WP_CRON', true);

Then, schedule wp-cron.php from your server. For Linux, that means:

crontab -e

However, instead of running it in Command Line (CLI), run it as an HTTP request. For that you may use wget:

*/5 * * * * wget -q -O - https://your-domain.com/wp-cron.php?doing_wp_cron

WordPress loads all the required core files, Plugins etc. in wp-cron.php with the following CODE:

if ( !defined('ABSPATH') ) {
    /** Set up WordPress environment */
    require_once( dirname( __FILE__ ) . '/wp-load.php' );

So don't worry about WordPress not loading important features.

  • 1
    WordPress.org documentation you linked to mentions wget http://YOUR_SITE_URL/wp-cron.php without the addition of ?doing_wp_cron So is one better than the other? What does the addition of ?doing_wp_cron do that the non-version doesn't? – Garconis Apr 26 '18 at 20:25
  • Probably just so your logs will show the query string so you know how it was called with certainty. – Slbox Apr 12 at 21:45

There are a couple of downsides: Firstly, Using wp-cron.php comes in as a cli call so things such as $_SERVER variables aren't set. People overcome this limitation by using a curl request to wp-cron.php instead.

Secondly, Because WP itself isn't loaded with wp-cron.php; if you use a SMTP mailer plugin then this won't be loaded when calling wp-cron. Again, using a curl call overrides this problem. Curl seems to be the most frequently used method.

However; I prefer to use wp-cli after setting mail settings in postfix and (for nginx) php-fpm config correctly and setting a crontab such as

*/5    *   *   *   *  wp cron event list --skip-plugins --skip-themes --path="/var/www/vhosts/example.com/httpdocs/wp" --fields=hook,next_run_relative --format=csv | awk -F, '$2=="now" {print $1}' | xargs -r wp --path="/var/www/vhosts/example.com/httpdocs/wp" cron event run $1

(List all crons with specific fields in csv format - hook being the name of the cron, next run relative is the time. Strip out ones showing 'now' as next run (ones due now) using AWK, pass that list to xargs to call wp cron event run $HOOK on each cron. ) Using wp-cli loads WordPress correctly (I choose to skip plugins when listing the crons, as code erros and php warnings will screw up the scripted output; but not to skip them when running the cron with xargs, as the cron may need the plugins being loaded)

Hope this gives you some pointers in what to look out for.

  • 1
    How about setting up: /15 * * * wget -q -O - yourdomain.com/wp-cron.php?doing_wp_cron as suggested by TomMcFarlin - tommcfarlin.com/wordpress-cron-jobs . Seems to do the job well. Would appreciate your comment. – TheBigK Jan 20 '17 at 17:19
  • Yeah as I mentioned throughout people choose to use curl (wget or any other http call) to trigger the crons, and there's nothing wrong with that method. I was just advising the problems of calling the wp-cron php file directly, which wouldn't include the required files, and advising another alternative method if you wanted to spice it up a bit. – TechnicalChaos Jan 20 '17 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.