Skip to content

WordPress Cron

Mark Howells-Mead edited this page Feb 28, 2024 · 27 revisions

Crontasks

  • Check and use the paths using which wp (CLI), not necessarily the paths in these examples!
  • Deactivate the WordPress cron call which is otherwise called on every page run by adding define('DISABLE_WP_CRON', true); to wp-config.php.
  • Edit the cronjobs via ssh using crontab -e. (List what's already active using crontab -l.)

Version with WP CLI

WP CLI needs to be installed on the server, e.g. in the ~/bin directory. (See these instructions - in German - for Cyon.)

Note that this example runs using a specific version of PHP. Make sure that the correct path is set to the PHP binary.

*/5 * * * * /usr/local/bin/php81 /PATH/TO/wp cron event run --due-now --quiet --path="/WEBROOT"

Version with wget

Don't forget --spider -q, otherwise every call will generate files like wp-cron.php.[1…] on the server.

> /dev/null 2>&1 stops any emails from being sent.

MAILTO="email@example.org"
*/5 * * * * wget -q --spider https://WEBSITE/wp-cron.php > /dev/null 2>&1

Multisite

PHP version

<?php
if ( !defined( 'ABSPATH' ) ) {
	require_once __DIR__ . '/wp-load.php';
}
if ( is_multisite() ) {
	$sites = get_sites();

	foreach ( $sites as $site ) {
		wp_remote_get( get_site_url() . $site->path . 'wp-cron.php?doing_wp_cron' );
		
		sleep( 3 );
	}
}

Then

*/5 * * * * wget -q --spider https://WEBSITE/sht-multisite-cron.php > /dev/null 2>&1

Shell version

Add as crontasks.sh and make executable with chmod +x crontasks.sh. Then call this file from the crontab using sh /full/webroot/path/crontasks.sh. This script will only work on network-enabled installations.

#!/bin/bash
# Copyright © 2015 Bjørn Johansen
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
#
# Via https://servebolt.com/help/article/how-to-setup-server-side-cron-for-wordpress-multisite/

WP_PATH="/full/webroot/path"

# Check if WP-CLI is available
if ! hash wp 2>/dev/null; then
echo "WP-CLI is not available"
exit
fi

# If WordPress isn't installed here, we bail
if ! $(wp core is-installed --path="$WP_PATH" --quiet --network); then
echo "WordPress is not network-installed here: ${WP_PATH}"
exit
fi

# Get a list of site URLs
if $(wp core is-installed --path="$WP_PATH" --quiet --network);
then
SITE_URLS=`wp site list --fields=url --archived=0 --deleted=0 --format=csv --path="$WP_PATH" | sed 1d`
fi

# Loop through all the sites
for SITE_URL in $SITE_URLS
do

# Run all event hooks that are due
for EVENT_HOOK in $(wp cron event list --format=csv --fields=hook,next_run_relative --url="$SITE_URL" --path="$WP_PATH" | grep now$ | awk -F ',' '{print $1}')
do
wp cron event run "$EVENT_HOOK" --url="$SITE_URL" --path="$WP_PATH" --quiet
#echo "Ran ${EVENT_HOOK} on ${SITE_URL}"
done
done

Update plugins via WP CLI

Update all plugins at patch version every weekday at 5 a.m., then notify of any available (minor and major) plugin updates ten minutes later.

0 5 * * 1-5 /PATH/TO/wp plugin update --all --patch --quiet --path=/WEBROOT/
10 5 * * 1-5 /PATH/TO/wp plugin update --all --quiet --dry-run --path=/WEBROOT/

wp-config.php

define( 'DISABLE_WP_CRON', true );
// define( 'WP_DEBUG', true ); // only on dev/staging
// define( 'WP_DEBUG_LOG', true ); // only on dev/staging
// define( 'WP_DEBUG_DISPLAY', false ); // only on dev/staging

Metanet

Create using Geplante Aufgaben, as crontab access via SSH isn't allowed. Also, the option Jetzt ausführen in Metanet's Plesk installation doesn't work with WP CLI commands. 🤦‍♂

/usr/bin/wp plugin update --all --patch --quiet --path=/home/httpd/vhosts/DOMAIN/httpdocs
/usr/bin/wp plugin update --all --minor --quiet --dry-run --path=/home/httpd/vhosts/DOMAIN/httpdocs
Clone this wiki locally