DEV Community

Cover image for Automate Wordpress setups via WP-CLI
Florian Stolzenhain
Florian Stolzenhain

Posted on • Updated on

Automate Wordpress setups via WP-CLI

Original post on my blog, happy to include feedback!
Cover: U.S. National Archives, via New Old Stock

--

These examples assume a working local WP-CLI helper –
for installing and file structure advice, see 4) installing WP-CLI.

  1. Setup vanilla Wordpress from scratch
  2. Migrate an existing instance
  3. Update an instance
    1. Specify version and locale
  4. See also
  5. Further reading

1) Setup vanilla Wordpress from scratch

Assumes an existing local database and host.

# root dir is project dir
mkdir -p wordpress
cd wordpress

# download to current folder, see
# https://developer.wordpress.org/cli/commands/core/download/
wp core download --locale=en_GB

# TODO: using the password on the shell is not ideal, see
# https://developer.wordpress.org/cli/commands/config/create/#examples
wp config create --dbname=[name] --dbuser=[user] --dbpass=[pass]

# this will return a user password
wp core install --url=website.local --title="Website title" --admin_user="janitor" --admin_email="foo@bar.local"

# create local wp-cli config for htaccess generation, see
# https://developer.wordpress.org/cli/commands/rewrite/structure/
printf "apache_modules:\n\
  - mod_rewrite\n" >> ../wp-cli.local.yml

# create htaccess
wp rewrite structure '/%year%/%monthnum%/%postname%/' --hard
Enter fullscreen mode Exit fullscreen mode

TODO: add SQLite example

2) Migrate an existing instance

wp db reset --yes
gunzip [file.sql.gz] --stdout | wp db import -

# reset pw of user `admin` to `test`
wp eval "wp_set_password( 'test', get_user_by( 'login', 'admin' )->ID );"

# set local url
# needs http[s]:// protocol and quotes
wp option update siteurl 'http://domain.local'
wp option update home 'http://domain.local'

# flush redirects
wp rewrite flush
Enter fullscreen mode Exit fullscreen mode

3) Update an instance

# update core
wp core update
# update all themes
wp theme update --all
# update all plugins
wp plugin update --all
# update language
wp language core update
wp language plugin --all update
wp language theme --all update
# update db scheme
# otherwise, an admin user will be prompted
wp core update-db
Enter fullscreen mode Exit fullscreen mode

Note: paradoxically, a working wp-config.php is mandatory for WP-CLI to work. You can use the helper command wp config create if you don't want to clone wp-config-sample.php manually.

Specify version and locale

wp core update --version=5.4 --locale=en_GB
Enter fullscreen mode Exit fullscreen mode

Check locale availability via WP translation platform

4) See also

Globally installing WP-CLI vs keeping a local version

You can

  1. move wp-cli.phar to a global wp binary

or

  1. keep a local install in you're projects root folder e.g. at /bin/wp-cli.phar

which makes sure each script continues to work on a remote installation.

Downloading core programmatically without WP-CLI

# grab and unpack the latest locale-specific release
curl https://de.wordpress.org/latest-en_GB.tar.gz -o latest-en_GB.tar.gz
tar -xvzf latest-en_GB.tar.gz
cd wordpress
Enter fullscreen mode Exit fullscreen mode

5) Further reading

Top comments (0)