Skip to content

Instantly share code, notes, and snippets.

@GillyRabutTsurwa
Created January 8, 2026 21:12
Show Gist options
  • Select an option

  • Save GillyRabutTsurwa/bd494257acff0a599df46fa4863be824 to your computer and use it in GitHub Desktop.

Select an option

Save GillyRabutTsurwa/bd494257acff0a599df46fa4863be824 to your computer and use it in GitHub Desktop.
bash script for updating wordpress sites
#!/bin/bash
wordpress_path="$HOME/Sites/$1"
log_file="$wordpress_path/logs/plugin-updates.txt"
git_commit_title="Plugin Updates"
github_remote="origin"
git_branch="$2"
# helper functions
function fail {
printf '%s\n' "$1" 1>&2 ## Send message to stderr.
exit "${2-1}" ## Return a code specified by $2, or 1 by default.
}
function create_array() {
local manual_plugins_file="$wordpress_path/manual-updateable-plugins.txt"
declare -n array="$1"
declare -n current_value="$2"
shift 2
while IFS= read -r current_value
do
skip=false;
while IFS= read -r current_manual_plugin
do
if [[ "$current_manual_plugin" == "$current_value" ]]
then
echo "Skipping update of $current_manual_plugin";
skip=true;
break;
fi
done <"$manual_plugins_file"
if [[ "$skip" == false ]]
then
array+=("$current_value");
fi
done < <("$@")
}
function script_exec_check() {
for current_arguement in "$@"
do
echo "$current_arguement";
done
if [[ $# -ne 2 ]]
then
echo "You do not have enough arguments to execute this script";
echo "Usage: <project_directory> <git_branch_name>";
echo "Stopping script execution";
exit 1;
fi
[ -z "$1" ] || [ -z "$2" ] && fail "you cannot do that";
}
function access_project() {
echo "Navigating to project directory...";
sleep 1.5s &>/dev/null;
cd "$wordpress_path" || exit 1;
if ! cd "$wordpress_path"
then
echo "This WordPress project does not exist";
exit 2;
fi
}
function verify_project() {
echo "Checking for git in the project...";
if [ -d "$wordpress_path/.git" ]
then
echo "Git check: Done";
else
echo "This is not a git project";
exit 3;
fi
sleep 2s &>/dev/null;
echo "Checking for core WordPress files...";
if [ -f "$wordpress_path/wp-config.php" ]
then
echo "WordPress check: Done";
else
echo "This is not a WordPress project";
exit 4;
fi
}
function git_branch_switch() {
if [[ $(git branch --show-current) != "$git_branch" ]]
then
echo "You are currently not in the $git_branch branch";
echo "Switching to $git_branch";
git checkout "$git_branch";
else
echo "Aready at the $git_branch branch";
fi
}
function convert_list_to_array() {
create_array plugins current_plugin wp plugin list --update=available --field=name;
create_array versions current_version wp plugin list --update=available --field=version;
plugins_count=$(wp plugin list --update=available --format=count);
}
function pre_update() {
if [[ ! -d logs || ! -f $log_file ]]
then
mkdir logs && touch "logs/$log_file";
if ! grep -q "logs" .gitignore
then
echo "logs" >>"$wordpress_path/.gitignore";
fi
fi
# Prepending Git commit title for log file
echo "$git_commit_title" >>"$log_file";
echo >>"$log_file";
}
function update_plugins() {
if [[ "$plugins_count" -eq 0 ]]
then
echo "All plugins are up to date";
truncate -s 0 "$log_file";
exit 3;
fi
echo "There are $plugins_count plugins that can be updated";
# looping over indeces in the array due to ${!plugins[@]} and not the values themselves. can still access the values.
for index in "${!plugins[@]}"
do
#wp plugin update "${plugins[index]}";
# if [[ $? -eq 0 ]]; then
if output="$(wp plugin update "${plugins[index]}")"
then
changelog_data_file="readme.txt";
version_updated=$(wp plugin get "${plugins[index]}" --field=version);
echo "$output";
echo "- ${plugins[index]} has been successfully updated from version ${versions[index]} to $version_updated" >>"$log_file";
if [[ -f "$wordpress_path/wp-content/plugins/${plugins[index]}/$changelog_data_file" ]]
then
git diff "$wordpress_path/wp-content/plugins/${plugins[index]}/$changelog_data_file" | grep '^+' | sed '/^+++/d' | sed 's/^+//' >>"$log_file";
echo >>"$log_file;"
fi
else
echo "${plugins[index]} failed up to update. Consult your WordPress admin panel to address this";
fi
done
echo "Updating plugins complete";
}
function commit_and_push() {
echo "Checking for updates to add to git";
sleep 3s &>/dev/null;
git add wp-content/plugins/ --all; # this will add everything in the wp-content/plugins directory recursively
if [[ $(git diff --staged --exit-code) ]]
then
echo "Initiating commit for plugin updates";
git commit --file "$log_file";
git push "$github_remote" "$git_branch";
else
echo "There are no plugin changes to commit";
fi
}
function cleanup() {
echo "If not plugins were updated try installing the remaining ones manually at your discretion";
echo "Note that plugins that require licence keys to be updated must be updated manually";
echo "Updating process complete";
# cleanup of log file
truncate -s 0 "$log_file";
}
#[ -z "$1" ] || [ -z "$2" ] && fail "One or both arguements are empty";
script_exec_check "$1" "$2";
access_project;
verify_project;
git_branch_switch;
convert_list_to_array;
pre_update;
update_plugins;
commit_and_push;
cleanup;
# praise Yahuah my Elohim
# may He bless and sancitfy this code
# in the Name of Yahusha
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment