Last active
October 29, 2024 08:59
-
-
Save hirasso/910ff58a1eccce7b64a385ddc1809c67 to your computer and use it in GitHub Desktop.
Install a WordPress plugin from the latests GitHub release
This file contains 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 | |
# Function to download and extract the latest release ZIP of a GitHub | |
# repository to the current WordPress install's plugin directory | |
download() { | |
# Split the first argument into repo_owner and repo_name | |
local repo_full="$1" | |
local repo_owner="${repo_full%/*}" | |
local repo_name="${repo_full#*/}" | |
local plugin_dir=$(wp eval 'echo WP_PLUGIN_DIR;') | |
echo "" | |
# Check if both parts are extracted correctly | |
if [[ -z "$repo_owner" || -z "$repo_name" ]]; then | |
echo "π‘ Usage: install-github-plugin <owner/repo>" | |
return 1 | |
fi | |
# Check if the plugin directory ends with '/plugins' | |
if [[ "$plugin_dir" != */plugins ]]; then | |
echo -e "β could not find wp plugin dir" | |
return 1 | |
fi | |
# Set the plugin directory from the second argument or default to the current directory | |
local final_dir="$plugin_dir/$repo_name" | |
# Check if the plugin directory already contains the folder | |
if [[ -d "$final_dir" ]]; then | |
echo "The folder $final_dir already exists. Exiting." | |
return 0 | |
fi | |
# Fetch the latest release zipball URL using GitHub API | |
local latest_release_url | |
latest_release_url=$(curl -s "https://api.github.com/repos/$repo_owner/$repo_name/releases/latest" | jq -r '.zipball_url') | |
# Check if the URL was retrieved successfully | |
if [[ "$latest_release_url" == "null" ]]; then | |
echo "Failed to get latest release for $repo_owner/$repo_name." | |
return 1 | |
fi | |
# Download the ZIP file | |
local zip_file="${repo_name}_latest.zip" | |
curl -L -s "$latest_release_url" -o "$zip_file" | |
echo "β Downloaded $zip_file" | |
# Extract the ZIP file to the plugin directory | |
unzip -q "$zip_file" -d "$plugin_dir" | |
echo "β Extracted $zip_file to $plugin_dir" | |
# Find the extracted directory name | |
local extracted_dir="$plugin_dir/$(unzip -Z -1 "$zip_file" | head -n 1 | cut -d/ -f1)" | |
# Rename the extracted directory to remove the "_latest" suffix, if needed | |
if [[ -d "$extracted_dir" ]]; then | |
mv "$extracted_dir" "$final_dir" | |
echo "β Renamed $(basename "$extracted_dir") to $(basename "$final_dir")" | |
else | |
echo "β Error: Extracted directory not found." | |
return 1 | |
fi | |
# Clean up by removing the ZIP file | |
rm "$zip_file" | |
echo "β Deleted $zip_file" | |
echo "" | |
echo "β Installed $repo_owner/$repo_name to $final_dir" | |
} | |
# Run the function with the provided arguments | |
download "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
This will download and install the latest release from a WordPress plugin hosted on GitHub
# in a wordpress install... chmod +x /path/to/install-wp-plugin-from-github-latest.sh /path/to/install-wp-plugin-from-github-latest.sh afragen/git-updater