That sounds fancy and high tech, doesn’t it? It’s almost like something you’d hear some hackers say on a TV show and make you think, “Woah, those dudes are smart!”
The CLI can be intimidating, especially if you aren’t a developer. Even as a developer, I avoided using the CLI for a very long time. But like a lot of awesome things, it’s actually far simpler than it seems. Let me clear up what the CLI is and why you should not just use it, but embrace it!
What Is a CLI?
CLIs come in all sorts of flavors. Most people who have been using computers for a couple decades (or more) might remember MS-DOS or the Command Prompt that comes with Windows. You may not have used it much but if you were using a Windows computer 20 years ago you probably had to pop it open and run some commands at some point. This is just one example of a Command Line Interface.
30 years ago, or so, I remember my family’s first computer, an Apple of some sort – no idea what the model was. You had to use the CLI to do anything since there was no graphical interface. In fact, Apple was actually the first company to popularize the drag and drop visual interface, the GUI.
GUI = Graphical User Interface
GUIs allow you to use your mouse and click around on all the fun icons and such. You use GUIs all day, everyday, on your phone, your computer, and many other everyday devices.
CLIs are kind of the opposite. All you see is a flashing cursor, and you have to explicitly type out what you want the computer to do. Computers are really good at doing complex things reliably and repeatedly, if you tell them line by line what you want.
For instance, if you’ve ever used The Command Prompt on Windows or Terminal on the Mac, then you have probably used the “cd” command. The “cd” command means “change directory.” On Macs and other Linux-based systems, the “ls” command tells the computer to list the contents of the directory/folder. Here’s an example of navigating to a photos directory and seeing its contents.
> IMG_001.jpg > IMG_002.jpg > IMG_003.jpg
OK, I Think I Get It, but What’s the WP-CLI?
Loads of applications and systems have their own CLI. CLIs are built to give users a command center for different software, OSs, tools, and more. The WP-CLI is a way to empower users with simple commands to perform big actions.
Note: The WP-CLI needs to be included with your server to use it. It’s not there by default. It’s a piece of software that must be installed. That said, it’s pretty widespread these days and included with most major WordPress-specific hosts and development tools, such as DesktopServer.
Using The WP-CLI
When I first heard about it and saw some of the use cases, I felt it was pointless. I wasn’t a fan of learning new CLIs and preferred pointing and clicking everything. I can update a user’s password in the WordPress Admin. Why the heck would I go through the trouble of using a CLI?
This attitude left me out of the loop for quite some time. The first time I started using the CLI regularly was when I was forced to use it. I joined a small web agency as a developer and the projects they put me on required admin access to the staging site. They told me, “You can log into the host’s website, and it gives you WP-CLI access. Just use that.”
“Sure, boss! Of course!,” I said.
What I really meant was, “Uhhhhhhhhh.” I had no clue how to do anything with the CLI but I was obstinate and didn’t ask for help. Fortunately the CLI is really freaking simple and it didn’t take much Googling to figure out.
> wp user add email@example.com --user_pass=####### --role=administrator
I’m in! Dang that was simple.
It’s Fast and Efficient
The fact remains, most of the things you can do with the CLI you can do with either the WP Admin or your host’s control panel. The advantage of the CLI is its speed and, more importantly, its efficiency. And for the more advanced developer, extensibility.
Avoid excess plugins
When we think of adding new functionality to WordPress installs, our first instinct is often to go see what plugins are available. Want to see a list of scheduled cron jobs? There’s a plugin for that.
Want to clear the cache? There’s a plugin for that.
Need to search and replace something in your database? There’s a plugin for that.
Need to export your database? There’s a plugin for that.
What’s one of the biggest rules for building a quality, professional WordPress website? Limit. Your. Plugins.
The more plugins you add, the more unknown code you have interacting, conflicting, and overlapping with each other. Plugins can be dangerous, and can add security vulnerabilities. So using only high-quality, actively maintained plugins is necessary. But even if all of your plugins are quality, they don’t always interact well with each other.
So why install four plugins to do simple things you could do with a tiny WP-CLI command?
> wp cron event list > wp cache flush > wp search-replace “olddomain.com” “newdomain.com” --all-tables > wp db export /home/backup.sql
See? Isn’t that much nicer?
Avoid breaking your website
Have you ever manually adjusted your WordPress database? It’s a bad idea. When you add/edit/delete content and settings in WordPress, a good chunk of the time there’s more to it than just changing a column’s value. People have crashed sites, and even all of WordPress.com by doing this.
When you use the WP-CLI it loads WordPress and performs commands like you are actually doing it through the WP Admin in your browser. This way when data is manipulated, it’s done the WordPress way and keeps things nice, tidy, and in order.
I won’t go into too much depth here, but the WP-CLI has a lot of very useful advanced development capabilities. I regularly write custom commands extending the tool’s functionality at my job. I wrote one recently that imported 30,000 users from an old non-WordPress database into the WordPress users system. It works extremely well and makes sure all users are created correctly.
I have one project where we like to regularly migrate the production database to staging, but QA wants to keep their test users on staging the same. I wrote a bash script that migrates between the servers and utilizes the WP-CLI to handle user setup, domain replacement, and more.
Just Give It a Shot
If I want you to take anything away from this article, it’s to not write off the WP-CLI as too advanced or unnecessary. Give it a shot. It can seem intimidating but just try it for something simple like backing up your database.
WordPress has acquired the CLI from the original developers. It’s here to stay and you should be happy about that. It’s already incredibly powerful and useful, and I’m excited to see it grow.
DesktopServer & WP-CLI
In the last two releases of DesktopServer, you might have noticed that the startup screen gives you several optional plugins (or extensions) you can load up. These plugins are design-time plugins, meaning that they are only activated and running in the background while you are working on your site locally. When you deploy a site to a live host, these plugins stay local and are not included with the live site. One of the plugins you’ll find in our Premium product is named DS-CLI. This is our own custom WP-CLI designed specifically to run in the DesktopServer environment. When activated at startup, ALL local development sites will now have CLI capability. Simple, right? No need to install the CLI package on each site and activate it. Once click and boom: all sites are set up with working CLI capabilities.
How Do I Access DS-CLI?
Once you’ve started up DesktopServer, there are two ways in which you can access CLI for a specific site (more information can be found by clicking this link):
- From the main menu in DesktopServer, click on the “Sites” button. This will open up your localhost page in your default browser.
- Hover over the name of your local site and you will see a link for DS-CLI pop up. Click on this link and your terminal (CLI) window will appear.
- From the WordPress dashboard on your local site, look in the admin bar at the top. You’ll find a DS-CLI link there. Click on this link and your terminal (CLI) window will appear.
Both options will take you to the same CLI, so it does not matter how you get there. It will also drop you into the correct directory for that site. Give it a go and let us know what you think!
Special thanks to Chris Flannagan at Modern Tribe for writing this guest post about DS-CLI. If you don’t know Chris, you should. He’s one of the awesome back end developers for MT who really knows his stuff. On top of that, he’s just a really great guy!
Additional props to the following contributors. Follow them on Twitter and let them know how much you appreciate their contributions to the Community!
I’m an Alabama native who’s travelled and lived all over this country and world. I ended up in Sarasota, Florida after grad school and have been here for 7 years. I began building websites in Junior High and never looked back. WordPress has been my passion for close to three years now.