Created
July 9, 2025 12:49
-
-
Save bhubbard/ed90e6c212002fc480d46846e68bdb4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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