How To Set Up And Use WP-CLI In WordPress
WP-CLI has been introduced to WordPress users and developers since around 2011. It aims to help users to manage their WordPress more easily. With WP-CLI, you can do almost anything simply by typing specific command. So in this article, I would like to share with you how to set up and use WP-CLI.
In order to set up and use WP-CLI on the remote environment such as your web server, you will need SSH access. Please check with your hosting provider whether your hosting plan includes SSH feature.
What Is SSH
SSH or known as Secure Shell is a network protocol that allows authorized users to securely access any computer or system over an unsecured network. It provides a secure way to connect any SSH client to SSH server using strong authentication and encrypted data communications.
What Is WP-CLI
WP-CLI or stand for WordPress Command Line Interface is a tool to make WordPress management a lot easier. If WordPress admin dashboard provides Graphical User Interface (GUI), then WP-CLI is its command line interface.
WP-CLI comes with many commands that allow you to easily manage your WordPress sites just with a single line. And here are some examples of tasks you can do with WP-CLI.
- Install a new WordPress site or reinstall your WordPress core files without affecting your contents, theme or plugins.
- Create, delete, list, update or generate your posts.
- Update, activate, deactivate, delete, manage or install your themes.
- Update, activate, deactivate, delete, manage or install your plugins.
- Export your contents to a WXR file or import your files as attachments, regenerates thumbnails, or lists registered image sizes.
- Create, update or delete users from your site as well as manages their roles, capabilities, and meta.
- Retrieves and sets your site settings including plugin settings.
- Search and replace strings in the database.
- Diagnose your WordPress problems.
- Identify your site performance issues.
How To Set Up And Use WP-CLI
Without further ado, let's get started by setting up our SSH access first.
Setting Up SSH
First, log in to your hosting control panel and open your SSH/Shell Access which usually located under Advanced section.
Once you open SSH, you will see a page similar like next image. Simply fill in all the fields to continue. For some unknown reason, you might not be able to use whitespace character in First and Last name fields.
Next, enter your email and strong password in their respective fields. Then, enter your IP address or any IP address you allow to use this SSH connection. You can also leave this field empty to allow all IP address, however, I wouldn't recommend that. After that, click on the Generate button to generate your SSH private key.
As soon as you click the Generate button, a pair of SSH key will be generated. Scroll down to the Manage current keys section and you will see the Public Key you just generated. Next, click on the Private Key button to continue. Please refer to the image below.
You will now see a lot of random characters on this new page. This is your encrypted SSH Private key. Please select all and copy everything on this textarea from "-----BEGIN RSA PRIVATE KEY-----" to "-----END RSA PRIVATE KEY-----".
Next, paste everything you have copied earlier on any text editor you prefer. Notepad, Notepad++ or Sublime are some of the most popular and widely used text editors. Then, save it as private_key.ppk.
As a reminder, please make sure it is saved as private_key.ppk and not as private_key.ppk.txt. If you are Windows user, you might want to enable the File name extensions option in Windows Explorer.
Setting Up WP-CLI For Windows User
Now you already have the credential for your SSH connection, next, let's download SSH client for Windows. There are many SSH client available but PuTTY is one of the most popular SSH clients. You can download PuTTY from the official website or directly from its author website.
After you download and install PuTTY, first, launch puttygen.exe and navigate to File » Load private key. Next, browse for your private_key.ppk, open it and enter the passphrase. The passphrase is the password you enter when you generate your SSH key previously.
Once your private key loaded, you will see a success message similar to the image below. Next, click on the Save private key button and save it using any name you like. But for the sake of this tutorial, I will name it as new_private_key.ppk.
Next, locate the file you just saved or in my case, it is new_private_key.ppk. Then, right click on this file and select Load in Pageant. Please take a look at the image below.
After that, launch putty.exe and you will see a new window called PuTTY configuration. Next, enter your server IP address and port on its respective field. By default, the SSH port is 22 but it may vary depending on your hosting provider.
As a side note, you can also save this session for future uses. Now, click on Open button to establish the connection to your web server.
Finally, type your cPanel username in the newly-opened command line interface and hit the enter button. And congratulation, now you are ready to experience the power of WP-CLI at your fingertips.
Now let's try to list all plugins using the WP-CLI command. Just type wp plugin list and hit the enter button. You might encounter an error saying "This does not seem to be a WordPress install". Don't worry as this is normal.
There are 2 reasons why you face this error. First, you have no WordPress installed yet or second, you are currently not in your WordPress directory.
If you already have WordPress installed, simply type dir to see where are you now. As for my case, I'm currently within the home directory of my hosting and my WordPress is installed in the public_html.
To go to my public_html, I just need to type cd public_html and you will see that the command line has changed now. Please refer to the image below.
If you follow the step above, you should already in your WordPress directory now. Unless, you have installed your WordPress elsewhere, for example, in the subdirectory. If that's the case, please go to your WordPress folder by using same way above.
For the side note, if you want to go up one level, you can use cd .. and hit enter. Now, let's try to list all plugins again by typing wp plugin list.
As you can see in the image above, there are 10 plugins available with only one is inactive. Let's try to activate it using WP-CLI command. Please type wp plugin activate wp-sweep and hit the enter button to continue.
As soon as you enter the command above, the plugin should have been activated and show you a success message. I'm sure right now you have understood how it works and know how to set up and use WP-CLI.
More About WP-CLI
The examples you have seen above are just a few of WP-CLI commands. There are a lot more interesting and useful commands available. For a complete list of WP-CLI commands, please check out their official website.
As can be seen, it is practically easy to set up and use WP-CLI on your WordPress. And I hope this article helps and gives you an idea about WordPress WP-CLI. It would also be great if you could share with us about your first experience when you set up and use WP-CLI.
He is a web developer, a programmer, and a computer technician. He obsessed with coding and enjoy learning new things. In his spare time, he likes to play online games, musical instruments or watching anime and movies.