EN DE

Studio Agenturbüro Studio Agenturbüro

⏏︎ / Archive / 2021 / WP-CLI

Automate Wordpress setups via WP-CLI

Created: 2021-02-16
Updated: 2023-09-20

These examples assume a working local WP-CLI helper –
for installing and file structure advice, see 4.2) 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

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

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

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.

3.1) Specify version and locale

wp core update --version=5.4 --locale=en_GB

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

5) Further reading



👉 Found an error or have a proposition?
Contact me via mail below – thanks!

--

Cover image: U.S. National Archives, via New Old Stock




© 2020–23 Studio Agenturbüro — contact@studioagenturbuero.comimprint, privacy policy, credits.