I am a long user of WP-CLI with SiteGround on Mac, when it was my first time using the WP-CLI, it took a couple of hours to set up with multiple attempts as I had to read SiteGround and WordPress documentation and watch videos, and now I come back to this tutorial to check some of the steps I did, and it works every time.
I’ve got multiple websites set up with WP-CLI on SiteGround, it’s a quick process for updating plugins, and it also gives a nice log of plugins required to be updated that I can save for before and after updates.
In this tutorial, I’ll show a quick WP-CLI setup on Mac that should only take less than 10 minutes if you don’t encounter any problems as I did with my first time.
Why use WP-CLI?
WP-CLI is an excellent WordPress tool to work in the terminal and automate your workflow to reduce the number of clicks you would have to do inside the WordPress dashboard.
Some uses to speed up your workflow:
- Add new users
- Update user roles
- Update Plugins and Themes
- Install new Plugins and Themes
- Deactivate Plugins and Themes
- Update WordPress
- Speed up Workflow on managing WordPress website/s
- Make you look like a hacker working with a terminal! Pretty cool.
- WP-CLI comes pre-installed on SiteGround, and updated by SiteGround
Setting Up SSH Key On Mac
Open your terminal on MAC and add the following command to generate new keys
ssh-keygen -t rsa
Next, you are asked to choose a location and enter the file name, you can save it in the default location or a new location, in this scenario I have used the hidden folder .ssh that may be available or you may have to create it. ( If it does not exist you can simply create it by opening a new terminal and typing in mkdir .ssh )
And straight after you are asked to enter a password and choose a strong password with which your private key can be accessed.
And now you will see two newly created files in your folder, private and public key(with .pub extension).
Keep the terminal open, we will still be using it in a bit.
Add Public Key to SiteGround
- Navigate to your Siteground tools
- Click on the Devs tab
- Click on SSH Keys Manager
- Click Import
Enter the name with your SSH key name, upload your public key by opening the lf.pub with a text editor, copy the key and paste it in manually.
Once the key is added, click import.
Load the Private Key
Now you’ve got to return to the terminal and add the following command line to add your private key.
Followed by a password that you have previously assigned when creating the ssh keys.
Now you should get a message with something like “Identity added”: .ssh/your-name
Connect to the website vis SSH
Now you can head back to the SiteGround Sitetools, navigate to SSH Keys Manager and where you have the public key added, click on the 3 dots menu, and get the credentials.
You will need to enter the username@hostname and provide the port to the website inside a terminal to access the website via SSH with the following terminal command, replace with your details.
ssh firstname.lastname@example.org -p18765
Note: I’ve encountered an issue that you should not come across with your first website, which gave an error “Too many authentication failures”, and I was able to fix it following this solution: https://cloudmaniac.net/how-to-fix-ssh-too-many-authentication-failures/
Now you should do something like this, and you connected to your SiteGround website via SSH!
To close the connection at any stage, you just need to enter the following command in the terminal:
Navigate to your WordPress directory via SSH
Now we can navigate to our WordPress directory.
First I usually start by checking the “www” directory, which will display the URL of your website, including, any staging websites there.
ls command is used to show all files located in the directory, you can use ls -a which will show all the hidden files that start wih a “.”(dot).
Navigate to the WordPress directory.
Check the directory to see your WordPress files
And you should get something like this with all the WordPress core files.
Working with WP-CLI
Checking WP-CLI Version
Let’s now check the current version of WP-CLI with the following command line
wp cli version
As I mentioned above, SiteGround keeps the WP-CLI updated on their side, so you don’t have to worry about updating the WP-CLI, but if you are running WP-CLI somewhere else you may need to install it, and keep it up to date.
That’s the command to update your WP-CLI if you have it do it somewhere else:
wp cli update
Running the WP command inside WP-CLI
You can check now all the commands that are offered by WP-CLI with the command line:
Once you run it, you will get a full list of commands that you can use inside the SSH WordPress directory.
Keep pressing Enter to reveal other commands, until you get to the end…
Once you are at the end it will show you some like
lines 112-135/135 (END)
Press “q” on your keyboard, and it will exit out of the wp command.
Let’s check for example what we can do with the “wp plugin” command, which will give us a list of available options
Checking Current Plugins
Check all the plugins that are on the website
wp plugin list
You can see I have for demonstration purposes a plugin that requires an update, active plugins, and inactive plugins.
Let’s add a new plugin to the Website, in this case, I need an SEO plugin for all my posts and going to install Rank Math, and activate it with one command line.
wp plugin install seo-by-rank-math --activate
We have installed a plugin to WordPress with one line, without having to go and login to the WordPress dashboard, and doing multiple clicks.
You can though go to your admin dashboard in WordPress and see for yourself that the Rank Math is installed, and activated.
This is the most useful feature for me in WP-CLI, that I can update all plugins with one command line, instead of having to click through various links to get to update the plugins, it’s a time saver, for individuals, and for agencies to get the task done quicker.
I do always recommend updating plugins first on a staging site before doing it on a production site, in case some errors may affect the user experience on the website.
Let’s update all plugins that we had from the previous list.
wp plugin update --all
Now you can have a cup of coffee while WP-CLI does all the updates for you.
Once it’s done, you will see a “Success” message.
As you can see I’ve accidentally used “-all” which it presumed was a plugin, what it requires is double hyphens to work “–all”.
Adding New User
WP-CLI is handy for creating new users, deleting users, updating roles, resetting user passwords and much more.
You can get the full list of commands with
Ok now let’s create a new user with a username, email and editor role, once created it will also provide a password for that user that you can share.
wp user create webdevartur email@example.com --role=editor
I prefer for security reasons not to handle other user passwords and instead could use a command like this that will send a user an email where they can follow a link to create their password handled by WordPress with the following command that will not show me the user’s password.
wp user create webdevartur firstname.lastname@example.org --role=editor --send-email --porcelain
WP-CLI allows you to do a lot more than the above-mentioned, you can even create a post and add images via terminal, but those tasks are easier done via the WordPress CMS Dashboard.
Other tasks may include updating the core of WordPress, updating the Theme, and configuring the wp-config.php file, for example, enabling and disabling file edits from the WordPress dashboard, activating maintenance mode, deactivating it and a lot more.
It’s a great way to speed up the process for an individual or an agency when managing a WordPress website.
You can go a further step, by automating your workflow by adding the most used commands to the shell, so you don’t have to retype the whole thing again to do the same tasks.