Last active
October 8, 2025 11:33
-
-
Save isuke01/1e8fe9cc15f31f526a3847139cc78719 to your computer and use it in GitHub Desktop.
Automation for Composer require WP plugin from wpackagist so you don't need to copy slugs bla bla bal
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 | |
| # wp-require — quickly require WP plugins/themes via wpackagist | |
| # Usage: | |
| # wp-require https://wordpress.org/plugins/onesignal-free-web-push-notifications/ | |
| # wp-require https://pl.wordpress.org/plugins/onesignal-free-web-push-notifications/ "^0.4" | |
| # wp-require https://wordpress.org/themes/twentytwelve/ | |
| # wp-require onesignal-free-web-push-notifications | |
| # wp-require twentytwelve --theme | |
| # wp-require my-cool-plugin "^1.2" --plugin | |
| set -euo pipefail | |
| usage() { | |
| echo "Usage: wp-require <wordpress.org URL or slug> [version-constraint] [--plugin|--theme]" >&2 | |
| exit 1 | |
| } | |
| command -v composer >/dev/null 2>&1 || { echo "Error: composer not found in PATH." >&2; exit 127; } | |
| [[ $# -ge 1 ]] || usage | |
| arg1="$1" | |
| version="" | |
| type="" | |
| shift | |
| # Parse optional args (version or flags) | |
| for arg in "$@"; do | |
| case "$arg" in | |
| --plugin) type="wpackagist-plugin" ;; | |
| --theme) type="wpackagist-theme" ;; | |
| ^*|~*|[0-9]*) | |
| version="$arg" | |
| ;; | |
| esac | |
| done | |
| # Trim trailing slashes | |
| url="${arg1%/}" | |
| slug="" | |
| # Detect from URL or treat as slug | |
| if [[ "$url" =~ wordpress\.org ]]; then | |
| # URL mode | |
| if [[ "$url" =~ /plugins/ ]]; then | |
| type="${type:-wpackagist-plugin}" | |
| slug="$(printf '%s' "$url" | sed -E 's#.*?/plugins/([^/]+).*#\1#')" | |
| elif [[ "$url" =~ /themes/ ]]; then | |
| type="${type:-wpackagist-theme}" | |
| slug="$(printf '%s' "$url" | sed -E 's#.*?/themes/([^/]+).*#\1#')" | |
| else | |
| echo "Error: Could not detect /plugins/ or /themes/ in URL." >&2 | |
| exit 2 | |
| fi | |
| else | |
| # Slug mode | |
| slug="$url" | |
| if [[ -z "$type" ]]; then | |
| # Default to plugin if not specified | |
| type="wpackagist-plugin" | |
| fi | |
| fi | |
| # Validate slug | |
| if [[ -z "$slug" || ! "$slug" =~ ^[a-z0-9-]+$ ]]; then | |
| echo "Error: Invalid slug: '$slug'" >&2 | |
| exit 3 | |
| fi | |
| package="$type/$slug" | |
| if [[ -n "$version" ]]; then | |
| echo "→ composer require $package \"$version\"" | |
| exec composer require "$package" "$version" | |
| else | |
| echo "→ composer require $package" | |
| exec composer require "$package" | |
| fi | |
| # 1. Add script to bin globally: | |
| # Create file and paste the content. | |
| # sudo mv wp-require.sh /usr/local/bin/wp-require | |
| # sudo chmod +x /usr/local/bin/wp-require | |
| # 2. Second option: Alias in .bashrc or .zshrc | |
| # BASH: | |
| # echo 'alias wp-require="bash /usr/local/bin/wp-require"' >> ~/.bashrc | |
| # source ~/.bashrc | |
| # ZSH: | |
| # echo 'alias wp-require="bash /usr/local/bin/wp-require"' >> ~/.zshrc | |
| # source ~/.zshrc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment