Using WP-CLI aliases to switch between multiple WordPress environments safely
Anyone managing several WordPress environments — production, staging, or separate installs for different languages — ends up re-typing SSH connection details and install paths every time they run a command. Building that connection string by hand each time invites mistakes: a copy-paste error, or reusing a stale path, can send a command to the wrong environment entirely. That risk matters most…
Managing multiple WordPress environments can be a hassle, especially when it comes to typing out connection details and installation paths for each command. These errors are even more dangerous when running write commands, as they could potentially affect the wrong environment. WP-CLI offers a solution with its alias feature, which allows users to assign a short name like @production to a set of connection details, including SSH targets and WordPress install paths. Once registered, the short name replaces the full connection string for every subsequent command.
These aliases are typically stored in two config files: wp-cli.yml in the project directory or ~/.wp-cli/config.yml in the home directory. The project-level file takes precedence if the same alias exists in both. When working across multiple environments, it's better to consolidate aliases in the home directory to avoid confusion.
To use an alias, simply run the command with the alias name, like wp @production plugin list. WP-CLI will then execute the command against the remote WordPress install over SSH, just as if it had been run locally.
For single environments, individual aliases can be bundled into group aliases for easier execution. For example, @all can be used to run a command against both @production and @staging. This makes it easy to see which result belongs to which site, providing a form of record-keeping.
However, when using aliases, it's important to keep a few things in mind. Name aliases so that the environment is obvious, and avoid using short forms that could become ambiguous. It's also a good practice to run wp cli alias list before executing anything unfamiliar to review the currently registered aliases and their targets. Finally, avoid running write commands through a group alias, as this could lead to unintended consequences if something goes wrong.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.