WordPress Reset

Header image for WordPress Reset

Wipe and rebuild a WordPress test site with a simple shell script.

4 minute reading time of 890 words (inc code)


If you’re managing multiple WP sites, or developing plugins and themes, then WP-CLI is extremely handy - you’re able to script and automate many checks and tasks that you’d otherwise be sitting around for, tediously clicking the same things every time.

As an addendum to my Test Content Generator plugin, this is a shell script I use to reset my test site with the aid of WP-CLI, and keep it clean when checking that something I’ve made still works with a new version of WP.


I’m assuming you’ve developing on a remote machine (either really remote, or just a VM) and so you’ve got passwordless SSH working, with the remote command restricted to wp if you’re paranoid, and possibly an SSH alias for convenience. And that your remote user on the development host has that wp command in their path, and is able to write to the path where your test site is.

If you’re still developing on your own local machine (and I’d hope not in this day and age, given this handy example article on virtual development is over a decade old), then you’ll need to strip out the SSH prefix and unquote each command.

The Script

# will greatly depend on how you normally connect to it

# eg /home/your_remote_user/public_html/test_site or /var/www/test_site etc

# eg https://test.site.local or http://test_site

# test title

# your test user
WP_EMAIL="<YOUR_ADMIN_EMAIL>" # doesn't need to be real

# wp-cli can get the right localisation of WP automatically, so you may as well use it

# a remote theme you want activate by default
# if you're a testing against a theme, you'll want be using this instead

# space separated list of remote plugins; t-c-g is required, the other three are personal preference
PLUGINS="test-content-generator classic-editor classic-widgets user-switching"

# space separated again; any local plugins you're using
# NOTE: the commands used below to wipe the test site leave the "plugins" and "themes" directories untouched
#       meaning that you can deploy your own plugins & themes separately, and reset the test site around them

# uncomment depending on your remote user privileges / if you've already made the test site once
# ssh ${SSH_CONNECTION} "mkdir -p ${WP_BASE}/"


# wipe (almost) everything and reinstall
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} db reset --yes"
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} core install --url=${WP_URL} --title='${WP_TITLE}' --admin_user=${WP_USER} --admin_password=${WP_PASSWORD} --admin_email=${WP_EMAIL} --locale=${WP_LOCALE} --skip-email"

# now clean the default "hello world" etc and any stray media files etc left in uploads/ 
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} site empty --uploads --yes"

# update the admin user with a few default preferences; adjust to your own wants
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} user update ${WP_USER} --rich_editing=false --syntax_highlighting=false --comment_shortcuts=false --show_admin_bar_front=false --use_ssl=true"

# then reactivate the WP theme you like (it won't redownload it each time btw)
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} theme install ${THEME} --activate"
# or uncomment this to use your own local theme instead
# ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} theme activate ${LOCAL_THEME}"

# and whichever plugins are always wanted
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} plugin install ${PLUGINS} --activate"

# if you have any local plugins that aren't yet in the global repo, uncomment this
# (ie. probably whatever plugin you're actually testing...)
# ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} plugin activate ${LOCAL_PLUGINS}"

# and a couple of quick checks while you're here
# NOTE: check-update won't install the update, just let you know one exists
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} core verify-checksums"
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} core check-update"

# now populate the test site
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} test users --amount=20"
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} test taxonomies --amount=50"
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} test images --amount=10"
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} test posts --amount=40"
ssh ${SSH_CONNECTION} "wp --path=${WP_BASE} test comments --amount=100"

Save it somewhere convenient as wp-reset.sh etc, give it executable permission and away you go.

Reply via email