Auto-install of WordPress Theme from CLI
by Eric on Mar.25, 2010, under Blog, Scripts, wordpress
If you're like me, and you still like to install your WordPress themes from the command line (or you were 3/4 asleep and forgot WordPress now has a nice, easy-to-use auto-installer built into the admin interface), you know it can be a pain. You have to download the file, unzip it, make sure it's right, delete the archive, chwon the files, etc... It can be a pain.
So, I created a nice little script to take care of all of that. wpthemeget will take a download URL from the WordPress Themes directory (in the format of http://(www.)wordpress.org/extend/themes/download/themename.ver.zip (or .tar.gz, or .tgz)), and will download, unarchive, chown, and remove the downloaded file, with no muss or fuss... The output?:
root@fyre [.../wp-content/themes]# wpthemeget http://wordpress.org/extend/themes/ download/motion.1.0.4.zip
Downloading theme motion.1.0.4.zip...
Theme is a zip archive...
Unzipping... [COMPLETE]
...Removing downloaded archive file... removed `motion.1.0.4.zip'
...Chowning files... [COMPLETE]
...Installation Complete!
No muss, no fuss. The only caveat is that you have to be in your themes directory when you run it. Future versions probably won't have this requirement, but hey, I hacked this out when I was barely awake, on heavy medication, and repeatedly falling asleep at the keyboard. At one point, I had to delete something like 40 lines of "zzzzzzzzzzz......" because I fell asleep with my finger on the key. Fortunately, vi treated it as all one paragraph so '<esc> dd' did the job. The code for this little script is after the break. Grab it, chmod it to exec, and drop it in /usr/local/bin, and it'll be good to go anywhere on your system, for all your blogs. Happy blogging!
# wpthemeget - WordPress Theme Installer
# by: Eric Scalf -- @ericscalf.com
# Licensed under SPL (Scalf Public License) v.10
# You are licensed to take, use, and modify this script as you wish, provided
# you give attribution to the original author, and do not use the script for commercial use
# or monetary gain, without first obtaining permission from the original author.
# failure to do so constitutes a breach of license, and you agree that each breach of license
# incurs a fine of $15,000 USD payable to the original author.
#Non-commercial use of this script is encouraged, as is non-commercial distribution.
#
#!/bin/bash
if [ -z "$1" ]
then
echo "You must enter an URL for theme download..."
echo "The scrit is called thus:"
echo $0 "<url>"
echo
exit 9
fi
echo -n "Downloading theme "
echo $(echo "http://wordpress.org/extend/themes/download/motion.1.0.4.zip" | cut -d/ -f7)"..."
wget $1 > /dev/null 2>&1
if [ -z $(echo "${1}" | egrep tar[.]gz\|tgz) ]
then
echo "Theme is a zip archive..."
ZFILE=$(echo "http://wordpress.org/extend/themes/download/motion.1.0.4.zip" | cut -d/ -f7)
echo -n "Unzipping... "
unzip ${ZFILE} > /dev/null
echo "[COMPLETE]"
echo -n "...Removing downloaded archive file..."
rm -fv ${ZFILE}
echo -n "...Chowning files... "
TUSER=$(echo ${PWD} | cut -d/ -f3)
chown -R ${TUSER}.${TUSER} * > /dev/null
echo "[COMPLETE]"
echo "...Installation Complete!"
echo ""
exit 0
else
echo "Theme is a .tar.gz or .tgz archive..."
ZFILE=$(echo "http://wordpress.org/extend/themes/download/motion.1.0.4.zip" | cut -d/ -f7)
echo -n "Unarchiving... "
tar -xzf ${ZFILE} > /dev/null
echo "[COMPLETE]"
echo -n "...Removing downloaded archive file... "
rm -fv ${ZFILE}
echo -n "...Chowning files... "
TUSER=$(echo ${PWD} | cut -d/ -f3)
chown -R ${TUSER}/${TUSER} * > /dev/null
echo "[COMPLETE]"
echo "...Installation Complete!"
echo ""
exit 0
fi
