Managing multiple WordPress sites can be a daunting task, especially when it comes to performing routine maintenance tasks like updates, plugin management, and backups. However, thanks to WP-CLI (WordPress Command Line Interface) and SSH (Secure Shell), administering remote WordPress sites has become much more efficient and streamlined.
In this article, we will explore how WP-CLI and SSH can revolutionize the way you manage your WordPress sites, making your life as a web administrator much easier.
What is WP-CLI?
WP-CLI is a powerful command-line tool that allows you to manage various aspects of your WordPress sites directly from the terminal. It provides a wide range of commands to perform actions such as installing and updating plugins/themes, managing users, generating content, and much more. WP-CLI operates using the WordPress REST API, enabling you to interact with your WordPress site without needing to access the admin dashboard.
Why SSH?
SSH, or Secure Shell, is a network protocol that provides a secure way to access and manage remote servers. It establishes an encrypted connection between your local machine and the server, allowing you to execute commands remotely.
SSH is widely used in the web development world for tasks such as server administration, file transfers, and remote debugging.
Setting up WP-CLI and SSH:
To start using WP-CLI and SSH for managing your WordPress sites, you’ll need to set up both tools on your local machine and the remote server. Here’s a quick overview of the process:
- Installing WP-CLI: WP-CLI is a command-line tool that runs on top of PHP. You can install it globally on your local machine using a package manager like Homebrew (for macOS/Linux) or Chocolatey (for Windows). Once installed, you can verify the installation by running the “wp” command in your terminal. You can find detailed installation steps on the official WP-CLI website (https://wp-cli.org/).
- Enabling SSH Access: To use SSH, you need to ensure that the remote server allows SSH connections. Most hosting providers offer SSH access, but you may need to enable it through their control panel or contact your hosting support. Once SSH access is enabled, you can connect to the server using the SSH client available in your operating system’s terminal. If SSH access is not enabled, check your hosting control panel or contact your hosting support to enable it.
Setting up WP-CLI Aliases
WP-CLI aliases provide a convenient way to execute commands on any WordPress installation by setting up shortcuts in your wp-cli.yml
or config.yml
file.
For example, let’s say you’re working on a client development environment using Vagrant and you’ve registered a new rewrite rule. To flush the rewrites inside your Vagrant-based virtual machine, you can simply run:
# Run the flush command on the development environment of your client
$ wp @client-dev rewrite flush
Success: Rewrite rules flushed.
Then, once you push the site to production, you can run:
# Run the flush command on the production environment of your client
$ wp @client-prod rewrite flush
Success: Rewrite rules flushed.
With WP-CLI aliases, you no longer have to go through the process of SSHing into machines, changing directories, and spending precious time just to access a specific WordPress installation. By specifying the machine to work with, WP-CLI handles the connection seamlessly, eliminating the need for manual setup.
Moreover, WP-CLI alias groups allow you to create sets of aliases. By defining a group, such as “@client_name
“, you can conveniently execute a command simultaneously across multiple configured example sites. This simplifies the process and allows for efficient management of multiple sites with a single command.
# Run the update check on both environments dev & production
$ wp @client-name core check-update
Success: WordPress is at the latest version.
Success: WordPress is at the latest version.
Aliases can be registered in your project’s wp-cli.yml
file, or your user’s global ~/.wp-cli/config.yml
file. See the example below:
# Aliases to several WordPress installs
# An alias can include 'user', 'url', 'path', 'ssh', or 'http'
@client-prod:
ssh: production@domain.com
user: production
path: /path/to/public_dir/
@client-dev:
ssh: vagrant@192.168.50.10
user: vagrant
path: /path/to/public_dir/
@local:
ssh: vagrant:default
@client:
- @client-prod
- @client-dev
Using WP-CLI aliases offers several benefits that save time and streamline tasks, especially when it comes to running batch operations. Here are a few reasons why WP-CLI aliases are great for managing WordPress sites efficiently:
- Time-saving convenience: WP-CLI aliases eliminate the need to manually SSH into individual machines or change directories to access specific WordPress installations. By setting up aliases, you can quickly switch between sites and execute commands without repetitive steps, saving valuable time.
- Seamless connection handling: Once you define the aliases in your configuration file, WP-CLI knows how to establish the actual connection to the specified machines. This automated connection handling simplifies the process and eliminates the need for manual intervention, making it easier to manage multiple sites efficiently.
- Batch operations: WP-CLI aliases enable you to create alias groups, allowing you to perform batch operations across multiple sites simultaneously. Whether it’s updating plugins, performing backups, or running other routine tasks, using alias groups eliminates the need to execute commands individually for each site. This streamlines the process and boosts productivity.
- Consistent commands across sites: With aliases, you can execute the same WP-CLI commands across different WordPress installations consistently. This ensures that your batch tasks or configurations are applied uniformly, reducing the chances of errors or inconsistencies that may arise from manual execution.
- Scalability and flexibility: As your WordPress site portfolio grows, managing numerous installations can become challenging. WP-CLI aliases provide a scalable solution that allows you to easily manage an expanding number of sites. You can add, modify, or remove aliases as needed, adapting to your evolving requirements with ease.
By leveraging WP-CLI aliases, you can harness the power of automation, streamline your workflow, and efficiently manage multiple WordPress sites. The time saved and the ability to run batch tasks seamlessly make WP-CLI aliases an invaluable tool for web administrators and developers.
Further reading material:
Awesome Blog. Good in depths and details regarding the main issues and probable solution
Thank you Rodrigo. Looking forward to meeting you in Ciudad de México.
Awesome article!! Great work.
Thank you Mario – Let me know if you have ideas for another blog post!
Great article, txs Salvador for always keeping us updated 🙌
Thank you Rolando. Hope Canada is not as cold these days.
Such a great article about this, sometimes, complex topic. I did share it with a folk who was struggling trying to use some of the things that are so well explained here, and he nailed it, then, highly recommended.
Thank you Arian. Looking forward to travelling to Merida this year!
Very useful information, I will be looking forward to more wp-cli Articles, and Cron Automation.
Thank you Byron. Its always rewarding to get feedback!
Hey Salva, this an amazing post, thanks for sharing.
Thank you Bayardo, this article came because of a conversation we had a few weeks back about using wp-cli to standardize the plugins versions across several sites 🙂
Thanks for the inspiration and if you have any other suggestions, let me know!
Great post Salvador! I assume you can use these scripts to automate tasks, am I right?
Great article máster. Thanks for the info.
Great article máster. Thanks for the info. You should send this as a Wordcamp talk.
Great work! I didn’t know about WP-CLI. This blog has inspired me to dive deeper into WordPress and search for more information.
Great post