Handy wordpress plugin and theme updater

Doing the plugin and theme updates in our wordpress setup is tedious at times as it does not allow FTP access (thank God!). I’ve automated the plugin update since a while back and finally I got around to extend and refine the script to also support themes (I think, haven’t had the possibility test it yet =)) and also support installing new plugins and themes.

Script available below. We’ve placed it in /root for easy access. The URLs used could in theory be anything but in general we use the wordpress.org provided plugins and themes and they follow a strict pattern with a directory inside the zip files and so on. Other plugin files might not, the script ignores that problem and you are on your own then =)

#!/bin/bash

workdir=$(mktemp -d /tmp/wpupdate.XXXXXX)
backupdir=$(mktemp -d /tmp/wpupdate."$(date +%Y%m%d)".XXXXXX)

cleanup () {
        rm -R "$workdir"
}
trap "cleanup" EXIT

wptarget=/usr/share/wordpress/wp-content/plugins/

help=no
if [[ $1 ]] && [[ $1 == "-t" ]]; then
        wptarget=/usr/share/wordpress/wp-content/themes/
elif [[ $1 ]] && [[ $1 == "-p" ]]; then
        wptarget=/usr/share/wordpress/wp-content/plugins/
else    
        help=yes
fi
if [[ $help == "yes" ]]; then
        echo "USAGE: $(basename "$0") (-p|-t) URL1 URL2 ... URLN"
        echo "      -p for plugin update"
        echo "      -t for theme update"
        exit
fi

shift

if (( UID != 0 )); then
        echo "ERROR: Script must be executed as root."
fi

for url in "$@"; do
        filename=$(basename "$url")
        echo "Fetching $filename"
        if ! wget -q -O "$workdir/$filename" "$url"; then
                echo "File could not be retrieved, URL correct?"
                echo "Aborted."
                echo
                continue
        fi
        unzip -q "$workdir/$filename" -d "$workdir"
        directory=${filename%%.*}
        chown -R www-data:www-data "$workdir/$directory"
        if [[ -d "$wptarget/$directory" ]]; then
                mv "$wptarget/$directory" "$backupdir"
                echo "Backup kept in: $backupdir/$directory"
        fi
        mv "$workdir/$directory" "$wptarget"
        echo "Done."
        echo
done
This entry was posted in Skunkworks and tagged . Bookmark the permalink. Both comments and trackbacks are currently closed.