Update WordPress Via WP-CLI

Updating WordPress via the CLI is pretty easy.

First make sure you have wp-cli installed. I’ll use my Linode as an example.

# wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
--2021-08-02 03:09:12--  https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Resolving raw.githubusercontent.com (raw.githubusercontent.com)...,,, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)||:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6094557 (5.8M) [application/octet-stream]
Saving to: ‘wp-cli.phar’

wp-cli.phar    100%[===========>]   5.81M  --.-KB/s    in 0.04s

2021-08-02 03:09:12 (140 MB/s) - ‘wp-cli.phar’ saved [6094557/6094557]

# chmod +x wp-cli.phar

# sudo mv wp-cli.phar /usr/local/bin/wp

Then it’s as simple as this:

# /usr/local/bin/wp core update

Updating to version 5.8 (en_US)...
Downloading update from https://downloads.wordpress.org/release/wordpress-5.8-no-content.zip...
Unpacking the update...
Cleaning up files...
File removed: wp-includes/css/dist/editor/editor-styles-rtl.css
File removed: wp-includes/css/dist/editor/editor-styles-rtl.min.css
File removed: wp-includes/css/dist/editor/editor-styles.css
File removed: wp-includes/css/dist/editor/editor-styles.min.css
4 files cleaned up.
Success: WordPress updated successfully.

Loop Through An Array

Today I needed to loop through the output of a command on the WP CLI. The most simple way I could think to do that was to save the output of the command into an array and loop through it.

What I did was use wp site list to get the subsites on a WordPress Multisite. I needed to pass each of those sites as an argument to a command and give it some time in between each execution since it was going to be database intensive. Here is what worked out well.


subsites=($(wp site list --field=url))

for i in "${subsites[@]}"
wp my_command --url="$i"
sleep 5

Save the above into a file.

  1. vi sites.sh
  2. Add code and then save. :wq
  3. chmod and make executable chmod 766 sites.sh
  4. Execute it ./sites.sh | tee site.log


Some of the amazing devs at work helped to modify the loop a bit to make it better for logging.


sites=($(wp site list --field=url --path=/var/www))
for i in "${sites[@]}"
    domain=$(echo "$i" | awk -F/ '{print $3}')
    wp my_command --url="$i" | tee /path/to/file-"$domain".log