facelordgists
4/24/2015 - 8:59 PM

Automatically update WordPress

Automatically update WordPress

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

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

This can be run in shell with two optional arguments:

sh ./wordpress_auto_update.sh <username> force

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

#!/bin/sh
# source:
# http://blog.noah.hearle.com/wordpress-server-wide-plugin-update/

# 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
username='*'
userpath='/home/*/public_html/'
force=false
 
if [ -n "$1" ]; then
    username="$1"
fi
if [ -n "$2" ] && [ "$2" == "force" ] && [ -n "$1" ]; then
    force=true
fi
 
if [ -n "$username" ] || [ "$username" != "" ] || [ -d /home/$username/public_html/ ]; then
    userpath="/home/$username/public_html/"
fi
 
find $userpath -maxdepth 2 -name wp-config.php -print0 | while read -d $'\0' file;
do
    if [ $force == true ] || [ $(grep -iEl "define\s?[(]\s?['\"]AUTO_UPDATE['\"],\s?['\"]?(1|true|plugins?|core)['\"]?\s?[)];" "$file") ]; then
        if [ $force == true ] || [ $(grep -iEl "define\s?[(]\s?['\"]AUTO_UPDATE['\"],\s?['\"]?(1|true)['\"]?\s?[)];" "$file") ]; then
                echo "Checking WordPress Core and Plugins ${file:0:${#file}-14}";
                wp --allow-root --path=${file:0:${#file}-14} core update;
                wp --allow-root --path=${file:0:${#file}-14} plugin update --all;
        else
            if [ $(grep -iEl "define\s?[(]\s?['\"]AUTO_UPDATE['\"],\s?['\"]?core['\"]?\s?[)];" "$file") ]; then
                echo "Checking WordPress Core ${file:0:${#file}-14}";
                wp --allow-root --path=${file:0:${#file}-14} core update;
            else
                echo "Checking WordPress Plugins at ${file:0:${#file}-14}";
                wp --allow-root --path=${file:0:${#file}-14} plugin update --all;
            fi
        fi
    fi
done;