WordPress Server-wide Plugin Update

Created a new bash script to update all installations of WordPress server-wide (core, themes and plugins). It works a treat!

#!/bin/sh

# Automatically update WordPress based on the local configuration of AUTO_UPDATE in the wp-config.php file of each WordPress installation.

# Usage: sh ./wordpress_auto_update.sh <username> (optional) <force> (optional)

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
force=false

cd ~;

if [ -n "$1" ] && [ "$1" != "" ]; then
        username="$1"
        if [ ! -d /home/$username/ ]; then
                echo "Directory not found: /home/$username/"
                exit;
        fi
else
        username='*'
fi

if [ -n "$2" ] && [ "$2" == "force" ] && [ -n "$1" ]; then
        force=true
fi
if [ -n "$username" ]; then
        userpath="/home/$username/*"
else
        userpath="/home/$username/public_html/"
fi

echo "Searching for WordPress installations at $userpath"

find $userpath -regextype posix-extended -regex '^/home/([a-z]\w+)/(public_html|dev.*)/([a-z]\w+/)?wp-config\.php$' -type f -print0 | while read -d $'\0' file; do
        path=${file:0:${#file}-14}
        username=$(sed -r 's#^/home/([^/]+)/.+$#\1#' <<< $path)
        if [ $force == true ] || [ $(grep -iEl "define\s?[(]\s?['\"]AUTO_UPDATE['\"],\s?['\"]?(1|true|plugins?|themes?|core)['\"]?\s?[)];" "$file") ]; then
                if [ $force == true ] || [ $(grep -iEl "define\s?[(]\s?['\"]AUTO_UPDATE['\"],\s?['\"]?(1|true)['\"]?\s?[)];" "$file") ]; then
                        echo "Checking WordPress Core, Themes and Plugins $path"
                        wp --allow-root --path=$path core update
                        wp --allow-root --path=$path theme update --all
                        wp --allow-root --path=$path plugin update --all
                        wp --allow-root --path=$path core language update
                else
                        if [ $(grep -iEl "define\s?[(]\s?['\"]AUTO_UPDATE['\"],\s?['\"]?core['\"]?\s?[)];" "$file") ]; then
                                echo "Checking WordPress Core $path";
                                wp --allow-root --path=$path core update;
                                wp --allow-root --path=$path core language update;
                        elif [ $(grep -iEl "define\s?[(]\s?['\"]AUTO_UPDATE['\"],\s?['\"]?themes?['\"]?\s?[)];" "$file") ]; then
                                echo "Checking WordPress Themes $path";
                                wp --allow-root --path=$path core update;
                        else
                                echo "Checking WordPress Plugins at $path";
                                wp --allow-root --path=$path plugin update --all;
                        fi
                fi
                chown -R --quiet --from=root.root $username.$username $path/{wp-*,index.*,license.*,readme.*,xmlrpc.php}
        fi
done;

To activate this, add this to wp-config.php:

/* WordPress Auto-update for core, themes and plugins */
define('AUTO_UPDATE', TRUE); // Accepted: TRUE, 'core', 'themes', 'plugins'

This can be run in shell, as root, with two optional arguments:

sh ./wordpress_auto_update.sh <username> force

This requires the WP CLI. It can run at set times using cron.

If you want your WordPress website installed and updated for free, get in touch.

Last updated on

5 thoughts on “WordPress Server-wide Plugin Update”

Leave a Reply

Your email address will not be published. Required fields are marked *