Cron for WordPress MultiSite

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 cron.sh.

#!/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.

2 thoughts on “Cron for WordPress MultiSite”

  1. Hola,

    Al margen de añadir en el wp-config.php la línea “define( ‘DISABLE_WP_CRON’, true );”

    ¿Qué sucede si el multisitio tiene dominios distintos ?
    Supongamos tudominio.es para castellano, que además es el sitio principal, y tudominio.gal para gallego.

    ¿Sería suficiente con añadir en el servidor dos peticiones?
    wget -q -O – https://tudominio.es/wp-cron.php?doing_wp_cron >/dev/null 2>&1
    wget -q -O – https://tudominio.gal/wp-cron.php?doing_wp_cron >/dev/null 2>&1

    ¿La llamada al cron del principal ejecuta tareas también de los sitios secundarios?
    ¿La llamada al sitio secundario realmente ejecuta tareas de ese sitio?

    Saludos

  2. Sí, en los casos en los que hay un WordPress MultiSite, el sistema de funcionamiento es básicamente usar el hostname / carpeta corespondiente a cada uno de los sitios. Con eso ya es suficiente.

    Eso sí, tendremos que lanzar tantos crones como sitios tenemos.

    Lo mejor, eso sí, sigue siendo usar WP-CLI en un Cron para WordPress MultiSite.

Comments are closed.