Skip to content

Instantly share code, notes, and snippets.

@r-a-y
Last active January 21, 2020 00:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-a-y/bf33d6822a3049ec2c74 to your computer and use it in GitHub Desktop.
Save r-a-y/bf33d6822a3049ec2c74 to your computer and use it in GitHub Desktop.
Bash function to quickly install / update a WordPress plugin / theme and commit the addition in Git. Shove this in your .bashrc.
# Bash function to quickly install / update a plugin / theme and commit the addition.
#
# Function should be run from the root WP directory.
#
# Requires:
# wp-cli - http://wp-cli.org; should be used as 'wp'
# git - duh'doy! :)
#
# Usage:
# wpadd -slug "slug-of-item" -new "1.0.0"
#
# slug - Plugin or theme slug that you want to install / update from wp.org
# type - Whether we're dealing with a plugin or theme. Either 'plugin' or 'theme'. Default: 'plugin'
# new - Version you want to install or update
# prev - Previous version of the plugin or theme. This is used only for the commit message.
# skip - Skips updating. Useful if you want to just add the files in Git and commit.
# Enter any value if you want to skip, otherwise omit this parameter.
# msg - (optional) Appends an additional message to the end of the commit message.
# debug - Echo the commands. Used for internal debugging.
#
# Example:
# wpadd -slug akismet -new 3.0.4 - Installs 'akismet' and commits the changes
# wpadd -slug akismet -new 3.0.4 -prev 3.0.3 - Updates 'akismet' to 3.0.4 and commits the changes detailing both versions
# wpadd -slug akismet -new 3.0.4 -prev 3.0.3 -skip 1 - Adds the 'akismet' directory in Git and commits the changes detailing both versions
function wpadd() {
local action="install"
local type="plugin"
local slug=""
local new=""
local prev=""
local message=""
local msg=""
local skip=""
local debug=""
local wp=""
local gitadd=""
local gitcommit=""
while test $# -gt 0; do
case "$1" in
-action)
shift
action=$1
shift
;;
-type)
shift
type=$1
shift
;;
-slug)
shift
slug=$1
shift
;;
-new)
shift
new=$1
shift
;;
-prev)
shift
prev=$1
shift
;;
-msg)
shift
msg=$1
shift
;;
-skip)
shift
skip=$1
shift
;;
-debug)
shift
debug=1
shift
;;
*)
echo "$1 is not a recognized flag!"
exit 1;
;;
esac
done
# prev
if [ "$prev" ]; then
action="update"
wp="wp $type $action $slug --version=$new"
else
wp="wp $type $action $slug"
fi
if [ -z "$skip" ]; then
if [ "$debug" ]; then
echo $wp
else
eval $wp
fi
fi
gitadd="cd wp-content/${type}s/$slug && git add -A . && cd ../../../"
if [ "$debug" ]; then
echo $gitadd
else
eval $gitadd
fi
if [ "$action" == "update" ]; then
message="-m 'Update $type: $slug'"
else
message="-m 'Install $type: $slug'"
fi
message+=" -m 'New version: $new'"
if [ "$prev" ]; then
message+=" -m 'Previous version: $prev'"
fi
if [ "$msg" ]; then
message+=" -m '$msg'"
fi
gitcommit="git commit $message"
if [ "$debug" ]; then
echo $gitcommit
else
eval $gitcommit
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment