Skip to content

Instantly share code, notes, and snippets.

@fsgreco
Last active November 7, 2023 17:52
Show Gist options
  • Save fsgreco/7a0b9e288454be8499a144caa5c47621 to your computer and use it in GitHub Desktop.
Save fsgreco/7a0b9e288454be8499a144caa5c47621 to your computer and use it in GitHub Desktop.
Fetch a list of active plugins from your production wordpress environment and create the command to install everything with wp-cli // This is an alternative to the Node.js script `generate-plugin-script.js`
#!/usr/bin/env bash
# MIT © Santiago Greco - fsgreco@hey.com
# This script fetches a list of active plugins from your production wordpress environment.
# Once it retrieves the list it creates a second script ready to run: `install-plugins.sh`
#
# For more context and information please consult the documentation of the entire project:
# docker-wordpress - https://github.com/fsgreco/docker-wordpress#sync-plugins
if ! command -v jq &> /dev/null; then echo "Please install 'jq'." && exit 1; fi
if [ -f ../.env ]; then
export $(grep -v '#.*' ../.env | xargs | envsubst )
fi
function generate_plugin_list() {
URL="https://${HOSTNAME}/wp-json/wp/v2/plugins"
PLUGIN_LIST=$(curl -s $URL -k -u $USERNAME:$APP_PASSWORD )
ACTIVE_PLUGINS=$(echo $PLUGIN_LIST | jq 'map( select(.status == "active") )')
MAPPED_LIST=$(echo $ACTIVE_PLUGINS | jq 'map( { plugin: (.plugin | sub("/.*";"") ), version } )' )
# Create the script `install-plugins.sh`
RESULTING_SCRIPT="./install-plugins.sh"
echo -e "#!/usr/bin/env bash \nsleep 5; \n" > $RESULTING_SCRIPT
chmod 775 $RESULTING_SCRIPT
echo $MAPPED_LIST | jq -c '.[]' | while read i; do # -c breaks the array returns by lines
package=$(echo $i | jq -j '.plugin')
version=$(echo $i | jq -j '.version')
echo "wp plugin install ${package} --version=${version} --activate" >> $RESULTING_SCRIPT
done
}
generate_plugin_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment