One of the usual recommendations that are made for WordPress for better optimization is not to use the native WP-Cron system, but to make calls using a task scheduler.
And although there are multiple ways to make requests for chronos, the optimal one is to use WP-CLI.
In a simple WordPress we could make a request such that:
*/5 * * * * wp cron event run --due-now --path=/home/example.com/ >/dev/null 2>&1
This code would launch all pending hooks every 5 minutes.
But, in a WordPress MultiSite you have to keep in mind that each of the subsites has to launch its own chrono… and, if you have 3 or 4 controlled, then you could add those 3 or 4 lines in the chrono… but what if you have sites that are being created or archived?
In this case we can make a small script that is responsible for making the requests. For example we can save it as
#!/bin/bash WP_PATH="/webs/example.com/" SITE_URLS=`php "$(which wp)" site list --fields=url --archived=0 --deleted=0 --format=csv --path="$WP_PATH" | sed 1d` for SITE_URL in $SITE_URLS do php "$(which wp)" cron event run --due-now --url="$SITE_URL" --path="$WP_PATH" --quiet done
With this code we basically do 2 steps.
The first of them is to take a list of all the websites that are in the WordPress Multisite, passing the route where the wp-config.php is.
Once we have the list, we go site by site and launch the pending chronos that there are for that site.
Now we can configure this cron as:
*/5 * * * * bash /home/cron.sh >/dev/null 2>&1
With this system we can program the chronos and it will not matter if sites are added or archived, since each time the task is launched it will take out the list of available sites that have not been archived or deleted.
About this document
This document is regulated by the EUPL v1.2 license, published in WP SysAdmin and created by Javier Casares. Please, if you use this content in your website, your presentation or any material you distribute, remember to mention this site or its author, and having to put the material you create under EUPL license.