Simple way to run WP Scheduled Crons

As we know, by default WordPress is using its own pseudo-cron logic. It just runs at background, and while visitors don’t notice it, actual cron jobs runs when they are browsing our website.

Due to performance issues some developers decide disabling this auto-run at background and use custom calling instead.

For this purpose they simple add

 define('DISABLE_WP_CRON', 'true');

to wp-config.php file and then they add real cron job to their server’s crontab. Like this

* * * * * php /var/www/html/wp-cron.php

So it solves all problems. But, not all actually. I have faced some plugin’s where scheduled tasks are using some front-end data which doesn’t exist in php-cli mode.

That’s why for those cases i use simple trick.

Hybrid Solution – Keeping WP_Cron disabled at front-end, but with one exception.

Here is my code in crontab:

#you can keep this as well * * * * * php /var/www/html/wp-cron.php
*/5 * * * * wget --delete-after -qO- https://www.SITENAME.com/?custom_cron=1

And here is my code in wp-config.php

if(empty($_GET["custom_cron"])) define('DISABLE_WP_CRON', 'true');

These 2 small codes are doing this:

  • They are keeping wp_cron disabled for all visitors. So, no any according slowing down.
  • They let wp_cron to be enabled when custom_cron GET parameter exists.
  • And each 5 minutes, crontab calls https://www.SITENAME.com/?custom_cron=1 URL and runs all tasks via wp’s own cron.
Get more useful WP tricks and snippets by subscribing to my mailclub.

Leave a Reply

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