Skip to content

Instantly share code, notes, and snippets.

@bhubbard
Created July 9, 2025 12:49
Show Gist options
  • Select an option

  • Save bhubbard/ed90e6c212002fc480d46846e68bdb4d to your computer and use it in GitHub Desktop.

Select an option

Save bhubbard/ed90e6c212002fc480d46846e68bdb4d to your computer and use it in GitHub Desktop.
#!/bin/bash
# A WP-CLI script to install performance and image-related plugins,
# and then regenerate all media library thumbnails.
# --- Configuration ---
# Exit immediately if a command exits with a non-zero status.
set -e
# --- Script Start ---
echo "πŸš€ Starting the image optimization and regeneration process..."
echo "-------------------------------------------------------------"
# Check if WP-CLI is installed and available
if ! command -v wp &> /dev/null
then
echo "❌ Error: WP-CLI could not be found. Please install it and make sure it's in your PATH."
exit 1
fi
# --- Plugin List ---
# An array of plugins to install. This makes it easy to add more later.
PLUGINS_TO_INSTALL=(
"performance-lab"
"webp-uploads"
"regenerate-thumbnails"
)
# 1. Install and Activate Required Plugins
echo "πŸ”Ž Checking and installing required plugins..."
for plugin in "${PLUGINS_TO_INSTALL[@]}"; do
if wp plugin is-installed "$plugin"; then
echo "βœ… '$plugin' is already installed. Ensuring it's active..."
wp plugin activate "$plugin"
else
echo "πŸ”Œ '$plugin' not found. Installing and activating now..."
wp plugin install "$plugin" --activate
echo "πŸ‘ '$plugin' has been successfully installed and activated."
fi
echo "" # Add a blank line for readability
done
echo "βœ… All necessary plugins are installed and active."
echo "-------------------------------------------------------------"
# 2. Regenerate all thumbnails
echo "πŸ–ΌοΈ Starting thumbnail regeneration for all images."
echo "⏳ This can take a very long time depending on the size of your media library."
echo "Please be patient and do not interrupt this process."
# The `wp media regenerate` command is provided by the 'regenerate-thumbnails' plugin.
# The '--yes' flag automatically confirms the action, which is essential for scripting.
wp media regenerate --yes
echo "-------------------------------------------------------------"
echo "βœ… Success! All image thumbnails have been regenerated."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment