Skip to content

Instantly share code, notes, and snippets.

@gbot
Last active September 1, 2021 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbot/8e646df73e0f49682ea2fd03090dc682 to your computer and use it in GitHub Desktop.
Save gbot/8e646df73e0f49682ea2fd03090dc682 to your computer and use it in GitHub Desktop.
Call wp-cron.php on WP Multisite installations
#!/bin/bash
# Call wp-cron.php on WP Multisite installations
# Requires WP-CLI
# Looks for WP_PATH in {user_home}/wp-cli.yml, otherwise must pass in as $1
# Set up cron job with crontab -e
# Use MAILTO to send output to email or write to a log file 'bash ~/wp_multisite_cron.sh >> crontab.log'
# MAILTO=admin@email.nz
# */1 * * * * bash ~/wp_multisite_cron.sh [WP_PATH]
if [[ ! -f /usr/local/bin/wp ]]; then
echo 'ERROR: WP-CLI not found!' && exit 1
fi
# Use either passed in WP_PATH or fetch from ~/wp-cli.yml
if [[ $1 ]]; then
wp_path="--path=$1"
else
if [[ ! -f ~/wp-cli.yml ]]; then
echo 'ERROR: wp-cli.yml not found! Exiting ...' && exit 1
fi
fi
# Get the default site for the network (we don't need this so, commented out)
# default_site=$( /usr/local/bin/wp option get siteurl $wp_path )
# fetch the network's site urls
site_urls=$( /usr/local/bin/wp site list $wp_path --fields=url --archived=0 --deleted=0 --format=csv | tail -n +2 )
# run the cron tasks
if [[ $site_urls != '' ]]; then
echo
echo "**** Running: WP Cron for ALL sites on Multisite network ****"
for site in $site_urls;
do
/usr/local/bin/wp cron event run --due-now --url="$site" $wp_path && echo -e "\t+ Finished crons for $site"
done
echo "**** Complete: WP Cron ****"
echo
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment