Automate Wordpress setups via WP-CLI
Tags: wordpress, automation, shell, cheatsheet
Published: 2023-11-03 15:56:57

[Original post](https://studioagenturbuero.com/archive/2021/wp-cli-cheatsheet/) on my blog, happy to include feedback!
Cover: [U.S. National Archives](https://www.flickr.com/photos/usnationalarchives/7158238104/), via [New Old Stock](https://nos.twnsnd.co/post/75491786304/train-depot-in-somewhere-kansas)

--

These examples assume a working local WP-CLI helper –
for installing and file structure advice, see [4) installing WP-CLI](#globally-installing-wpcli-vs-keeping-a-local-version).

1. [Setup vanilla Wordpress from scratch](#1-setup-vanilla-wordpress-from-scratch)
2. [Migrate an existing instance](#2-migrate-an-existing-instance)
3. [Update an instance](#3-update-an-instance)
	1. [Specify version and locale](#specify-version-and-locale)
4. [See also](#4-see-also)
5. [Further reading](#5-further-reading)

## 1) Setup vanilla Wordpress from scratch


Assumes an existing local database and host.

```sh
# 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

```sh
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

```sh
# 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.

### Specify version and locale

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

Check locale availability via [WP translation platform](https://translate.wordpress.org/)

## 4) See also

### Globally installing WP-CLI vs keeping a local version

You can

1. [move `wp-cli.phar` to a global `wp` binary](https://wp-cli.org/#installing)

or

2. 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

```sh
# 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

- alternative to downloading core: the [Wordpress Skeleton](https://github.com/markjaquith/WordPress-Skeleton)
- applying `--allow-root`: [php - Setting WordPress siteurl using wp-cli with WAMP virtual host - Stack Overflow](https://stackoverflow.com/questions/39670418/setting-wordpress-siteurl-using-wp-cli-with-wamp-virtual-host)
- [13 Useful WordPress SQL Queries You Wish You Knew Earlier](https://onextrapixel.com/13-useful-wordpress-sql-queries-you-wish-you-knew-earlier/)