Skip to content

Renaming a Plugin

Brian Henry edited this page Sep 17, 2021 · 8 revisions

Often you might decide the name you gave to the plugin is not as succinctly descriptive as you might like. The following commands rename a plugin:

Close your PhpStorm window first. Then enter the following commands in a Terminal window.

old_plugin_name="Example Plugin"
new_plugin_name="New Name For Example Plugin"
old_plugin_slug=$(echo $old_plugin_name | tr '[:upper:]' '[:lower:]' | sed 's/ /-/g');
old_plugin_snake=$(echo $old_plugin_name | tr '[:upper:]' '[:lower:]' | sed 's/ /_/g');
old_plugin_package_name=$(echo $old_plugin_name | sed 's/ /_/g');
old_plugin_capitalized=$(echo $old_plugin_name | tr '[:lower:]' '[:upper:]' | sed 's/ /_/g');
old_test_site_db_name=$old_plugin_snake"_tests"; # TODO: 64 character limit.
old_test_db_name=$old_plugin_snake"_integration"; # TODO: 64 character limit.
old_plugin_db_username=${old_plugin_slug:0:31};
old_plugin_db_password=$old_plugin_slug;

new_plugin_slug=$(echo $new_plugin_name | tr '[:upper:]' '[:lower:]' | sed 's/ /-/g');
new_plugin_snake=$(echo $new_plugin_name | tr '[:upper:]' '[:lower:]' | sed 's/ /_/g');
new_plugin_package_name=$(echo $new_plugin_name | sed 's/ /_/g');
new_plugin_capitalized=$(echo $new_plugin_name | tr '[:lower:]' '[:upper:]' | sed 's/ /_/g');
new_test_site_db_name=$new_plugin_snake"_tests";
new_test_db_name=$new_plugin_snake"_integration";
new_plugin_db_username=${new_plugin_slug:0:31};
new_plugin_db_password=$new_plugin_slug;

Delete all files that can be recreated

rm -rf wp-content;
rm -rf src/vendor;
rm -rf src/dependencies;
rm -rf vendor;
rm -rf wordpress;

rm -rf .idea;
# Rename database names and passwords in .env.testing and .sql files
# Start with longest first so sub-strings don't unintentionally match (i.e. 32 character limit on username).
find . -type f \( -name '.env.testing' -o -name '*.sql' \) -exec sed -i '' "s/$old_plugin_db_username/$new_plugin_db_username/g" {} +;
find . -type f \( -name '.env.testing' -o -name '*.sql' \) -exec sed -i '' "s/$old_test_site_db_name/$new_test_site_db_name/g" {} +;
find . -type f \( -name '.env.testing' -o -name '*.sql' \) -exec sed -i '' "s/$old_test_db_name/$new_test_db_name/g" {} +;
find . -type f \( -name '.env.testing' -o -name '*.sql' \) -exec sed -i '' "s/$old_plugin_db_password/$new_plugin_db_password/g" {} +;


find . -depth -name '*'$old_plugin_slug'*' -execdir bash -c 'git mv "$1" "${1//'$old_plugin_slug'/'$new_plugin_slug'}"' bash {} \;
find . -depth \( -name '*.php' -o -name '*.txt' -o -name '.env.testing' -o -name '*.md' \) -exec sed -i '' "s/$old_plugin_name/$new_plugin_name/g" {} +;
find . -type f \( -name '*.php' -o -name '*.txt' -o -name '*.json' -o -name '*.xml' -o -name '.env.testing'  -o -name '*.yml' -o -name '.gitignore' -o -name '.htaccess' -o -name '*.md' \) -exec sed -i '' "s/$old_plugin_slug/$new_plugin_slug/g" {} +;
find . -depth \( -name '*.php' -o -name '*.testing' \) -exec sed -i '' "s/$old_plugin_snake/$new_plugin_snake/g" {} +;
find . -type f \( -name '*.php' -o -name '*.txt' -o -name '*.json' -o -name '*.xml' \) -exec sed -i '' "s/$old_plugin_package_name/$new_plugin_package_name/g" {} \;
find . -depth -name '*.php' -exec sed -i '' "s/$old_plugin_capitalized/$new_plugin_capitalized/g" {} +;
find . -type f \( -name '.env.testing' \) -exec sed -i '' "s/$old_plugin_db_username/$new_plugin_db_username/g" {} +;

Then the directory

cd ..; mv $old_plugin_slug $new_plugin_slug; cd $new_plugin_slug; 

Create the new database.

mysql_username="root";
mysql_password="secret";
export $(grep -v '^#' .env.testing | xargs);

source .env.testing

# For MySQL
mysql -u $mysql_username -p$mysql_password -e "CREATE USER '"$TEST_DB_USER"'@'%' IDENTIFIED WITH mysql_native_password BY '"$TEST_DB_PASSWORD"';";


# For MariaDB:
mysql -u $mysql_username -p$mysql_password -e "CREATE USER '"$TEST_DB_USER"'@'%' IDENTIFIED BY '"$TEST_DB_PASSWORD"';";



# Create the databases.
mysql -u $mysql_username -p$mysql_password -e "CREATE DATABASE "$TEST_SITE_DB_NAME"; USE "$TEST_SITE_DB_NAME"; GRANT ALL PRIVILEGES ON "$TEST_SITE_DB_NAME".* TO '"$TEST_DB_USER"'@'%';";
mysql -u $mysql_username -p$mysql_password -e "CREATE DATABASE "$TEST_DB_NAME"; USE "$TEST_DB_NAME"; GRANT ALL PRIVILEGES ON "$TEST_DB_NAME".* TO '"$TEST_DB_USER"'@'%';";

Delete the old database

mysql_username="root";
mysql_password="secret";

mysql -u $mysql_username -p$mysql_password -e "DROP DATABASE IF EXISTS "$old_test_db_name";";
mysql -u $mysql_username -p$mysql_password -e "DROP DATABASE IF EXISTS "$old_test_site_db_name";";
mysql -u $mysql_username -p$mysql_password -e "DROP USER '"$old_plugin_db_username"';";

mysql -u $mysql_username -p$mysql_password $TEST_SITE_DB_NAME < tests/_data/dump.sql;

composer install

vendor/bin/wp option set home http://localhost:8080/$new_plugin_slug;
vendor/bin/wp option set siteurl http://localhost:8080/$new_plugin_slug;

mysqldump -u $TEST_SITE_DB_USER -p$TEST_SITE_DB_PASSWORD  $TEST_SITE_DB_NAME > tests/_data/dump.sql;
open -a PhpStorm ./;

composer install;
vendor/bin/codecept run acceptance;
Clone this wiki locally