Skip to content

Backgrounds

How-to Guides

Technical References

VIP-CLI /

Advanced usage

Skipping command confirmation

When a command against a production environment is attempted, a prompt will appear in the terminal to confirm the action. To skip this confirmation step pass the --yes (or -y) option before the -- command separator.

vip @mytestsite.preprod --yes -- wp option get siteurl

Bypassing the command confirmation is useful when a command is part of a script.

For nested commands run on a production environment, the --yes option is required in both inner and outer commands.

vip @mytestsite.preprod --yes -- wp post delete $(vip @mytestsite.preprod --yes -- wp post list --post_type='page' --format=ids)

Writing command output to a file

All WP-CLI commands are run on a remote web server. File operations initiated by a command will occur on the remote server as well with no way to retrieve the file.

It is recommended to write the output to the console first using helpers like WP_CLI::line() and WP_CLI::log(), then redirecting the output to a specific file:

vip @mytestsite.preprod --yes -- wp option get siteurl > output.txt

See more detailed guidance on saving command output to a file.

Referencing a file input

To reference an input file in a command, commit the file to a publicly accessible location within the application’s GitHub repository, like the themes or plugins directories. The path of the file can then be referenced with a path similar to this example:
/var/www/wp-content/themes/my-theme/<file>.

Automation

VIP-CLI can be used for automating common flows and tasks both locally and in CI contexts (e.g. triggering Data Sync on a schedule; running WP-CLI commands across all environments). To do this, the authentication prompt can be bypassed by creating a config file.

  1. Generate a new token in the VIP Dashboard (select “Set up CLI“).
  2. Create a file named ~/.config/configstore/vip-go-cli.json.
  3. Add the following code to vip-go-cli.json, replacing VIP-USER-TOKEN with the token retrieved from the VIP Dashboard.
{
  "vip-go-cli": "{VIP-USER-TOKEN}"
}

When setting up automation for VIP-CLI commands, it is recommended to create a bot/machine user and generating a token for that machine user account.

Nested commands

When running more complex WP-CLI commands such as loops or nested commands, each reference to wp must use its own remote execution pattern.

In the examples below, a nested command is structured to include two WP-CLI commands with the following functions:

  1. wp post list to retrieve a list of post IDs
  2. wp post delete to delete the retrieved post IDs

This will not work:

The format of this nested command example will not work as intended because the command inside the $() is referencing the local version of wp (i.e. on the local computer):

$ vip @mytestsite.preprod -- wp post delete $(wp post list --post_type='page' --format=ids)
-bash: xyz: command not found

Running the command in the above format might only return a message indicating  wp does not exist locally. However, if the command is run from within a local WordPress site directory, it may have a more destructive outcome by retrieving post IDs from the local WordPress site and passing those IDs to the delete command to be run against the remote site.

This will work:

vip @mytestsite.preprod -- wp post delete $(vip @mytestsite.preprod -- wp post list --post_type='page' --format=ids)

Each WP-CLI command must be run as its own VIP-CLI command. In addition, for production environments, the --yes flag must be included in both the inner and outer commands to bypass the confirmation prompts:

vip @mytestsite.production --yes -- wp post delete $(vip @mytestsite.production --yes -- wp post list --post_type='page' --format=ids)

Looping

Because of the way that shell utilities handle stdin, commands like xargs may not always work with WP-CLI as expected. In these cases it might be easier to write the commands to a file and then execute that file.

This will not work:

vip @mytestsite.preprod -- wp post list --fields=ID --format=csv --posts_per_page=5 | xargs -I % 'vip @mytestsite.preprod -- wp post update % --post_category=NewCategory '

This will work:

$ vip @mytestsite.preprod -- wp post list --fields=ID --format=csv --posts_per_page=5 | xargs -I % sh -c 'echo "vip @mytestsite.preprod -- wp post update % --post_category=NewCategory" >> commands.sh '
$ chmod +x commands.sh
$ ./commands.sh 

Last updated: January 11, 2022