For many users, the best part of using WordPress is the intuitive and user-friendly graphical interface. To build content on a page, you simply type it in like you would in a Microsoft Word document. To update a plugin, you click update. To change a setting, you just go to settings and make the change. In fact, you can perform every administrative action through the graphical interface. So, given all of these user-friendly features, why would we want to use a command line interface? Let’s explore that.
A lot of the usefulness of the WordPress CLI (command line interface) is dependent upon what you are trying to accomplish. Sometimes, it is easier and actually the better approach to simply do what you need to do using the graphical interface. Learning to use the CLI effectively involves both learning how to use it and learning when not to use it. Doing simple actions like updating plugins or WordPress core is probably better to do using the graphical interface, though it is especially easy to do using the CLI and only takes a single command. Other actions like creating a single post or editing a menu are also probably easier to do using the graphical interface. So, let’s look at some situations where the CLI is the better option. But before we begin, make sure you properly install the WordPress CLI.
Regenerating Thumbnails
A common action in WordPress is to regenerate thumbnails when you add a new image size to your WordPress theme. This is a prime example of the CLI greatly simplifying a process. To do this action on the graphical interface, you actually need to download a separate plugin. In the CLI, it’s a matter of entering a single command and the action is done. That command is:
wp media regenerate
You can even further specify which images to regenerate by adding an additional parameter after the regenerate command. Here is some more information on this command.
Working with the Database
Database operations are a major part of WordPress development. With the WordPress CLI, you can perform any query with the following command:
wp db query {QUERY}
This allows you to quickly perform operations on the tables in your database without having to deal with the graphical interface of your database client. You can even import and export SQL files and schedule tasks to run automatically. Here is some more information on this command.
Search and Replace
Local development is a common practice in WordPress development. It speeds up the development process since there is no need to transfer files to your server every time you make a change. It also allows the developer to test new features without fear of accidentally breaking something on their website. A crucial aspect of local development involves finding and replacing the URLs for your site in the database with your local URL. There are a number of graphical approaches for doing this, whether through a plugin or by operating directly on the database. This gets tricky though, since values in a WordPress database are often serialized, and a simple find and replace is not enough. The WordPress CLI really shines in this scenario. Using the search-replace command, you can perform a database-wide operation that will also handle serialized data! It’s as simple as doing the following command:
wp search-replace ‘livesite.com’ ‘localsite.com’
Now, you just saved potentially hours of trying to figure out how to replace serialized data by entering a single command. To be extra safe, you can also run this command as a dry run to see how many instances are found and whether they look right. Just run the following command:
wp search-replace ––dry-run ‘livesite.com’ ‘localsite.com’
Here is some more information on this command.
Working with Multiple Sites
WordPress developers are often in charge of multiple WordPress sites, and sometimes they have to perform the same action on most or even all of them. They might be adding a plugin to all of their sites or updating a setting. Performing actions on multiple sites is one of the best uses for the WordPress CLI. Because the CLI is used inside a shell environment, you can combine CLI commands with the shell scripting language of your choice to automate tasks such as these. Let’s look at adding a plugin to all of your sites. You could write a Bash script similar to the following:
#!/bin/bash
for wp_site in $(wp site list ––field=url)
do
wp plugin install {plugin name} ––url=$wp_site
done
Now, instead of going into the dashboard for every one of your sites and installing the plugin, you run this script and the job is done. You can also add further parameters such as ––activate to the end of that command to automatically activate the plugin after installing.
Conclusion
The WordPress CLI is as useful as you make it. You can easily get by without ever using it since every administrative action can be performed using the front-end graphical interface provided by WordPress. However, development is as much about learning to do tasks efficiently as it is about the end result. A good rule of thumb is when you find yourself doing the same task over and over again, ask yourself if there’s a better way to do it or even if you can automate the task entirely. While it might take more time initially to write a script or track down the correct commands, in the long run, time is ultimately saved. Plus, as a developer, there’s a certain level of satisfaction that comes from typing a few commands and executing a complex series of tasks. Simply pressing a button on the graphical interface will never capture that feeling.