webSIGHTdesigns - Web Design, Web Development, Web Hosting

WordPress Update Script

Posted on Monday, October 14th, 2013 at 5:54 pm
by webSIGHTdesigns

This bash script will allow you to update your WordPress core files and plugins.

  • This Bash script requires curl and zip be installed on the system
  • Create a file containing this script in /usr/local/bin/wpupdate
  • Set the new file to be executable with chmod +x /usr/local/bin/wpupdate
  • To run the script, assuming /usr/local/bin is in your $PATH, just cd into the root directory of your WordPress install, and run wpupdate
  • Run the command wpupdate -h to view the help.
  • Run the command wpupdate -f to update the core files and all plugins, regardless of whether they need upgrading or not.
#!/bin/sh
# wpupdate v1.0 by @websightdesigns
set -e # exit script on any error
if [ "$1" = '-h' ]; then
cat << EOF
Usage: wpupdate [option]
This script updates the current directory's WordPress core files and plugin files.
The default behavior is to update all WordPress core files and plugins, unless they can be determined to already be the latest version.
The -f option can optionally be used to force updating all plugins and core files.
However, currently plugins which are just one single file (not a directory of files like most plugins) are updated every time.
OPTIONS:
-h Show this message
-f Force updating all WordPress core files and plugins even if they are already the latest version
EOF
exit
fi
# get version of currently installed wordpress files
version=$(grep "wp_version = " ./wp-includes/version.php|awk -F\' '{print $2}')
# get the latest version of wordpress
latest=$(curl -s http://api.wordpress.org/core/version-check/1.5/ | head -n 4 | tail -n 1)
# check if this version of wordpress exists
wpversion=$(curl --write-out %{http_code} --silent --output /dev/null http://core.svn.wordpress.org/tags/$version/)
if [[ ! $version = $latest ]] && [[ $wpversion == "200" ]]; then
	updatewp=true
else
	updatewp=false
fi
if [[ $1 == "-f" ]]; then
	updatewp=true
	updateall=true
fi
# update wordpress core files
if [ $wpupdate ]; then
	touch ./.maintenance
	curl -O http://wordpress.org/latest.zip
	unzip latest.zip
	if [ -d "./wp-content/" ]; then
		rm -rf ./wordpress/wp-content/
	fi
	if [ -d "./wordpress/wp-config.php" ]; then
		rm ./wordpress/wp-config.php
	fi
	cp -rf ./wordpress/* .
	rm -rf ./wordpress/
	rm latest.zip
	rm ./.maintenance
fi
# remove example plugin if it exists
if [ -f "./wp-content/plugins/hello.php" ]; then
	rm ./wp-content/plugins/hello.php
fi
# remove example config if it exists
if [ -f "./wp-config-sample.php" ]; then
	rm ./wp-config-sample.php
fi
# update all plugins
if [ -d "./wp-content/plugins/" ]; then
	webroot=$(pwd)
	# folder plugins
	for i in $(find $webroot/wp-content/plugins/ -maxdepth 1 -mindepth 1 -type d)
	do
		f=$(basename $i)
		if curl --output /dev/null --silent --head --fail "http://downloads.wordpress.org/plugin/$f.zip"; then
			if [ ! $i = "./wp-content/plugins/" ]; then
				cd "$webroot/wp-content/plugins/"
				# get the plugin readme and use that to calculate the latest stable release
				if curl --output /dev/null --silent --head --fail "http://plugins.svn.wordpress.org/$f/trunk/readme.txt"; then
					curl --silent -o $f-readme.txt http://plugins.svn.wordpress.org/$f/trunk/readme.txt
					echo "http://plugins.svn.wordpress.org/$f/trunk/readme.txt"
					tr -cd '\11\12\40-\176' < $f-readme.txt > $f-readme-tr.txt
					pluginv=.`awk '/Stable/ {print $3}' $f-readme-tr.txt`
					echo "$f - $pluginv"
				fi
				if curl --output /dev/null --silent --head --fail "http://plugins.svn.wordpress.org/$f/trunk/README.txt"; then
					curl --silent -o $f-readme.txt http://plugins.svn.wordpress.org/$f/trunk/README.txt
					if [ -s "$f-readme.txt" ]; then
						echo "http://plugins.svn.wordpress.org/$f/trunk/README.txt"
						tr -cd '\11\12\40-\176' < $f-readme.txt > $f-readme-tr.txt
						pluginv=.`awk '/Stable/ {print $3}' $f-readme-tr.txt`
					fi
				fi
				if [[ "$pluginv" == ".trunk" ]] || [[ "$pluginv" == "." ]] || [[ $updateall == "true" ]]; then
					updateplugin=true
				else
					if [ -f "./$f/readme.txt" ]; then
						tr -cd '\11\12\40-\176' < ./$f/readme.txt > readme-tr.txt
					elif [ -f "./$f/README.txt" ]; then
						tr -cd '\11\12\40-\176' < ./$f/README.txt > readme-tr.txt
					fi
					currpluginv=.`awk '/Stable/ {print $3}' ./readme-tr.txt`
					if [[ ! "$currpluginv" = "$pluginv" ]]; then
						updateplugin=true
					fi
				fi
				if [[ "$updateplugin" == "true" ]]; then
					curl -O "http://downloads.wordpress.org/plugin/$f.zip"
					unzip -o "$f.zip"
					if [ -f "$f.zip" ]; then
						rm -f "$f.zip"
					fi
				fi
				rm $f-readme.txt $f-readme-tr.txt
				if [ -f "readme-tr.txt" ]; then
					rm readme-tr.txt
				fi
			fi
		fi
	done
	# single-file plugins
	for i in $(find $webroot/wp-content/plugins/ -maxdepth 1 -mindepth 1 -type f)
	do
		f=$(basename ${i%.*})
		if [ ! "$f" = 'index' ]; then
			if curl --output /dev/null --silent --head --fail "http://downloads.wordpress.org/plugin/$f.zip"; then
				cd "$webroot/wp-content/plugins/"
				curl -O "http://downloads.wordpress.org/plugin/$f.zip"
				unzip -o "$f.zip"
				if [ -f "$f.zip" ]; then
					rm -f "$f.zip"
				fi
			fi
		fi
	done
fi
# eof

Notes

This bash script can be used to change into the root directory of a WordPress website and run a command to update the core WordPress files and any plugins. It does not support plugins that are not in the WordPress Plugins directory.

This script checks to see if it can determine a plugin's latest version number by downloading the readme.txt before downloading the rest of the files. It's also sometimes found at README.txt sometimes. Unfortunately though, these readme files aren't 100% reliable, many times they simply list the latest stable version as ".trunk" in which case we choose to update the given plugin.

Currently plugins which are just one single file (not a directory of files like most plugins) are updated every time.

Please Sign In

Please sign in to post a comment.

Web Development

View details »

Web Hosting

View details »

Our Portfolio

View portfolio »

WebSight Designs webSIGHTdesigns preferred email webSIGHTdesigns United States United States