Skip to content

Instantly share code, notes, and snippets.

@rocketgeek
Last active November 8, 2022 14:24
Show Gist options
  • Save rocketgeek/6afc179e982a0ce5b531b2c8ba66ed8c to your computer and use it in GitHub Desktop.
Save rocketgeek/6afc179e982a0ce5b531b2c8ba66ed8c to your computer and use it in GitHub Desktop.
batch file: create/delete wordpress site in xampp
:: This is for spinning up installs for dev work, so don't consider this for
:: production applications. It creates everything off a single entry variable
:: so that you don't have to mess with unnecessary details. The site name and
:: username are the same (a single input), as are the database and db user.
:: The passwords for the initial WP user and the db user are generic "pass".
:: PHP, WP-CLI, and MYSQL all need to be path environment variables.
:: If you get errors that indicate something is not recognized as an internal
:: or external command, make sure you have these in your PATH.
@echo off
:: Start in the XAMPP folder, assumes XAMPP is in the root (i.e. "c:/xampp/)
:: (use PUSHD to get into the dir instead of "cd" so we can use POPD to get back into the original dir).
PUSHD "C:/XAMPP/htdocs"
:: Ask for package folder. Everything is built off this: subfolder name, WP user, db user, db name.
set /p wp_name=WP Install Name:
:: Create Database (uses MySQL CLI)
echo Creating database and db user
call mysql -e "CREATE USER '%wp_name%'@'localhost' IDENTIFIED BY 'pass';" -u root
call mysql -e "GRANT ALL PRIVILEGES ON *.* TO '%wp_name%'@'localhost';" -u root
call mysql -e "FLUSH PRIVILEGES;" -u root
call mysql -e "CREATE DATABASE %wp_name%;" -u root
:: Create folder in XAMPP/htdocs folder.
echo Creating C:/XAMPP/htdocs/%wp_name%/ folder
mkdir %wp_name%
cd %wp_name%
:: Download WordPress (uses WP-CLI)
call wp core download
:: Use WP-CLI to create the wp-config.php file
echo Creating wp-config.php file
call wp config create --dbname="%wp_name%" --dbuser=root --dbpass="" --dbhost=localhost --dbprefix="%wp_name%"
:: Install WordPress using WP-CLI
echo Installing WordPress
call wp core install --url="localhost/%wp_name%" --title="%wp_name%" --admin_user="%wp_name%" --admin_password=pass [email protected] --skip-email
:: We're done, print out access details.
echo Done installing WordPress.
echo Access at http://localhost/%wp_name%/
echo Log in at http://localhost/%wp_name%/wp-admin
echo username: %wp_name%
echo password: pass
:: Go back to original folder (when using PUSHD to get into the dir, POPD takes us back).
POPD
@pause
:: PHP, WP-CLI, and MYSQL all need to be path environment variables.
:: If you get errors that indicate something is not recognized as an internal
:: or external command, make sure you have these in your PATH.
@echo off
:: Start in the XAMPP folder, assumes XAMPP is in the root
:: (use PUSHD to get into the dir instead of "cd" so we can use POPD to get back into the original dir).
PUSHD "C:/XAMPP/htdocs"
:: Ask for package folder.
set /p wp_name=WP Install Name:
:: Delete folder in XAMPP directory.
echo Deleting %wp_name% folder
@RD /S /Q %wp_name%
:: Delete Database and users (uses MySQL CLI)
echo Deleting db user
call mysql -e "DROP USER '%wp_name%'@'localhost';" -u root
call mysql -e "FLUSH PRIVILEGES;" -u root
echo Deleting database %wp_name%
call mysql -e "DROP DATABASE %wp_name%;" -u root
echo Done remvoing WordPress site %wp_name%.
:: Go back to original folder (when using PUSHD to get into the dir, POPD takes us back).
POPD
@pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment