Skip to content

Instantly share code, notes, and snippets.

@gsarig
Last active December 25, 2023 18:18
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 gsarig/54f38f7082f7dae1054b275be00e3e9f to your computer and use it in GitHub Desktop.
Save gsarig/54f38f7082f7dae1054b275be00e3e9f to your computer and use it in GitHub Desktop.
Batch update WordPress sites using WP-CLI
#!/bin/bash
# A bash script to batch update a server's WordPress sites with WP-CLI. It requires WP-CLI, obviously (https://wp-cli.org/).
# The script will search for all WordPress installations under a given directory and will update them. You can declare more than one such directories.
# You don't have to modify the script every time you add a new site. As long as the new site is under a declared parent directory, its database will be backed up.
# After creating the file, don't forget to make it executable by doing:
# chmod +x update_sites.sh
# Set PATH environment variable
export PATH="/usr/local/bin:/usr/bin:/bin"
# The base path on your server (you might need to change that)
BASEPATH=/var/www
# We use a function, in order to be able to call it as many times as we want, for as many different directories containing WordPress installations there are on the server
update_sites() {
# Iterate over all folders under the given parent directory
for d in $1/* ; do
# Check if the folder is indeed a WordPress installation by searching for a wp-config.php file
if [ -f $d/wp-config.php ]; then
echo -e "\e[32mUpdating $d\e[0m"
# Update all plugins
sudo /usr/local/bin/wp --path="$d/htdocs" --allow-root plugin update --all
# Update core
sudo /usr/local/bin/wp --path="$d/htdocs" --allow-root core update
# Update database after core update
sudo /usr/local/bin/wp --path="$d/htdocs" --allow-root core update-db
# Update core themes (the ones with their filename starting with "twenty")
for theme in $(sudo /usr/local/bin/wp --path="$d/htdocs" --allow-root theme list --field=name | grep twenty);
do sudo /usr/local/bin/wp --path="$d/htdocs" --allow-root theme update $theme;
done
fi
done
}
# Call the function giving as a parameter the path to the directory containing your WordPress installations (you might need to change that)
update_sites ${BASEPATH}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment