Last active
March 29, 2020 15:24
-
-
Save Genyus/363c555ba6d03c25c96a89f0041cd93e to your computer and use it in GitHub Desktop.
Bash script to create WordPress plugin using modified WordPress Plugin Boilerplate
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 | |
# Constants | |
git_url=https://github.com/karannagupta/WordPress-Plugin-Boilerplate/archive/master.zip | |
archive=WordPress-Plugin-Boilerplate-master | |
# Save present working directory | |
pwd=$(pwd) | |
# Ask the user for details | |
read -p 'Plugin installation path (e.g. /Users/user/Sites/my-site/wp-content/plugins): ' plugin_path | |
if [ -z "$plugin_path" ]; then | |
echo "Plugin installation path is required. Please re-start command." | |
exit 1 | |
fi | |
read -p 'Plugin name (e.g. My Awesome Plugin): ' plugin_name | |
read -p 'Plugin description (e.g. This plugin does awesome stuff): ' plugin_description | |
read -p 'Plugin URL (e.g. https://somedomain.com/my-awesome-plugin-uri): ' plugin_uri | |
read -p 'Plugin slug (e.g. my-awesome-plugin): ' plugin_slug | |
read -p 'Plugin namespace (e.g. My_Awesome_Plugin): ' plugin_namespace | |
read -p 'Author name (e.g. Awesome Dev): ' author_name | |
read -p 'Author URL (e.g. https://somedomain.com): ' author_uri | |
echo | |
# Create temporary folder | |
tmp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir') | |
cd $tmp_dir | |
# Download plugin repo | |
curl -sLJO $git_url | |
# Extract archive | |
unzip -q "$archive".zip | |
cd "$archive" | |
# After having downloaded and extracted the archive, navigate to the folder containing the plugin | |
mv wp-plugin-name "$plugin_path" | |
cd "$plugin_path" | |
# Rename files/folders with the text "wp-plugin-name" in them | |
if [ -n "$plugin_slug" ]; then | |
rename_command="mv {} \$(echo {} | sed \"s/wp-plugin-name/$plugin_slug/\")" | |
find . -depth -iname "*wp-plugin-name*" -execdir sh -c "$rename_command" \; | |
else | |
plugin_slug='wp-plugin-name' | |
fi | |
# Navigate to plugin folder | |
cd "$plugin_slug" | |
# Escape slashes for text replacement | |
plugin_name="${plugin_name//\//\/}" | |
plugin_description="${plugin_description//\//\/}" | |
plugin_uri="${plugin_uri//\//\/}" | |
author_uri="${author_uri//\//\/}" | |
# Replace text for "example.com/wp-plugin-name-uri" and "example.com" | |
if [ -n "$plugin_uri" ]; then | |
grep -rl "http://example.com/wp-plugin-name-uri" ./* | xargs sed -i '' -e "s/http:\/\/example.com\/wp-plugin-name-uri/$plugin_uri/g" | |
fi | |
if [ -n "$author_uri" ]; then | |
grep -rl "http://example.com" ./* | xargs sed -i '' -e "s/http:\/\/example.com/$author_uri/g" | |
fi | |
# Replace text for "wp-plugin-name" | |
if [ "wp-plugin-name" != "$plugin_slug" ]; then | |
grep -rl "wp-plugin-name" ./* | xargs sed -i '' -e "s/wp-plugin-name/$plugin_slug/g" | |
fi | |
# Replace Namespace references for the text "WP_Plugin_Name" | |
if [ -n "$plugin_namespace" ]; then | |
grep -rl "WP_Plugin_Name" ./* | xargs sed -i '' -e "s/WP_Plugin_Name/$plugin_namespace/g" | |
fi | |
# Replace text for Your Name | |
if [ -n "$author_name" ]; then | |
grep -rl "Your Name or Your Company" ./* | xargs sed -i '' -e "s/Your Name or Your Company/$author_name/g" | |
fi | |
# Replace text for WordPress Plugin Boilerplate | |
if [ -n "$plugin_name" ]; then | |
grep -rl "WordPress Plugin Boilerplate" ./* | xargs sed -i '' -e "s/WordPress Plugin Boilerplate/$plugin_name/g" | |
fi | |
# Replace plugin description | |
if [ -n "$plugin_description" ]; then | |
grep -rl "This is a short description of what the plugin does. It's displayed in the WordPress admin area." ./* | xargs sed -i '' -e "s/This is a short description of what the plugin does\. It's displayed in the WordPress admin area\./$plugin_description/g" | |
fi | |
# Clean up | |
rm -R $tmp_dir | |
cd "$pwd" | |
# Notify user of completion | |
echo "Plugin boilerplate installed at $plugin_path/$plugin_slug" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment