If you do a lot of WordPress development work, you may find it useful to have ways to quickly spin up WP sites for testing and development work. Personally, Docker and the wp-env are the best ways to do that (IMO), but a lot of people like to use XAMPP for local development work. If you use XAMPP, these batch files will give you a quick way to spin up a WP site for development and a fast way to delete the site.
- XAMPP is installed (it is assumed that XAMPP is installed in the c: root. Modify the bat files accordingly if it is not).
- PHP, WP-CLI, and MySQL are all in the PATH environment variable so that they can run from the command line. If you don’t know what this means, I’ll have another post to help you get set up to use these from the command line.
This is intended for dev purposes – at least as it is written. It’s a simple single input, and it uses that input value to name the directory, the site, the user, the database, and the database user. For example, if you input “dev1”, then that will be used for all of those values. The password for the WP user and the db user will be “pass”. Obviously, you can change this and expand on the batch file as needed for your personal use, but keep in mind that as written this isn’t for creating production sites (and the reasons should be obvious).
Additionally, it also assumes your XAMPP MySQL uses the “root” user with no password. You can alter this as well as necessary.
Create a site
:: 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. echo WordPress site name (use a single term as this is used echo for all terms: user, database, folder name, etc.) set /p wp_name=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 --firstname.lastname@example.org --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
Run this from the command line, powershell, or however you want to run a batch file (I actually use VS Code with the Batch Runner extension, more on that in another article).
Here what the command line output would look like using this to create a “test1” site:
Again, this is just defaults… But you should get the idea. This created a site called “test1” that we can now access at http://localhost/test1 and log in using username “test1” and password “pass”. It will be in the c:/xampp/htdocs/test1 folder and we can do whatever development we need to there – we can test plugins, develop themes, or whatever.
Remove a site
We can use the other batch file to remove a site we created with the above batch.
:: 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 site to remove: :: 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
Note that use of this batch file assumes you used the “create” batch file to create the site in the first place.
Here’s the command line output after running the delete: