How to manually trigger WP-Cron through a Laravel Forge scheduled job
When hosting WordPress sites through Laravel Forge, I always get issues with WP-Cron. Because of these issues, I can't use the scheduled backup feature of Updraft. This can be fixed by creating a scheduled job that will run all WP Cron events.
I love Updraft as an easy way to back-up client sites (in addition to complete server backups of course). However, when trying to set up Updraft on a WordPress site hosted through Laravel Forge, I always get WP Cron errors.
Adding define('DISABLE_WP_CRON', true); to your wp-config.php should be the first thing you try, but in my case this didn't fix the issue.
Fortunately, Laravel Forge makes it very easy to schedule commands.
Using WP CLI to run WP-Cron events
I'll be using WP CLI to run all WP-Cron events that are due. The command is pretty easy:
cd /path/to/wordpress/folder && wp cron event run --due-now
Easy, right? This will check if any WP-Cron events are due (or have missed their schedule) and execute them right away.
In my case: this will run all the Updraft backups that have missed their schedule.
Creating the scheduled job in Laravel Forge
- Go to Laravel Forge and click on your server
- Go to 'Sites' and choose your WordPress site
- Choose 'Scheduler'
You'll see your list of scheduled jobs. Laravel Forge provides some default scheduled jobs out of the box.
Click on 'New Scheduled Job'.
Paste the command in the field. Make sure to choose the right user (especially if you're using isolated sites). You can easily choose a predefined frequency, or use 'Custom' to configure it exactly how you want to.
That's it! Creating a scheduled job for WP-Cron is actually a great idea for all your sites, because it's more reliable than using the default WP-Cron functionality. By default, the WP-Cron will only be triggered when someone visits your website. So if you don't have any visitors, WP-Cron will never run. By using a scheduled job, you make sure WP-Cron gets triggered regularly.
A note on 'disable_wp_cron'.
If you're triggering wp-cron on your server through a scheduled job, it's a good idea to set 'disable_wp_cron' to 'true' in your wp-config.php. This should boost the performance of your website. In fact, disabling WP-Cron and manually triggering it on your server should probably be your go-to approach for all sites anyway!