WP-CLI is a command-line tool for WordPress website developers and maintainers, allowing them to manage common (and not so common!) activities such as adding or removing users, articles, categories, as well as performing searches and replacements in the database… All of this in a targeted or bulk manner according to one’s needs.
It is a stable project that has nearly ten years of development behind it and whose source code is hosted, like many other open-source software including the same WordPress, on the platform GitHub.
The WP-CLI is a tool that web professionals should not refrain from using. Let’s see how to install and use it right away.
Table of contents
Requirements for installing WP-CLI
Let’s first see what the system requirements are to be able to install the WP-CLI:
- Unix-like Environment (OS X, Linux, FreeBSD, Cygwin)
- PHP > 5.6
- WordPress >= 3.7, keeping in mind that as the WordPress version grows, more features become available
How to install WP-CLI
To install WP-CLI it is advisable to always refer to the official site, and the procedure we report below is at the end of 2021:
Download the executable file:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Check the integrity:
php wp-cli.phar --info
Give execution permissions:
chmod +x wp-cli.phar
We move to a standard folder for system executable files:
sudo mv wp-cli.phar /usr/local/bin/wp
Verify that everything went smoothly with a generic command that returns the WP-CLI version and other installation details:
wp --info
In subsequent updates, if we wish to make an update, it will be sufficient to launch:
wp cli update
Once installed, we can navigate to the folder hosting a WordPress site and begin launching commands for all operations we need.
It is good to keep in mind that commands are always referred to the installation of the current folder (with the possibility of operating on WordPress “multi-site”, where multiple sites coexist in a single folder with the same files) and if the CLI detects that there is no WordPress installation in the file system location where we find ourselves, it will throw an error.
General syntax
WP-CLI is a classic command-line tool: commands always start with “wp” followed by a command and a subcommand, which are in turn followed by mandatory and optional parameters, with or without values.
The general syntax is therefore of this type:
wp command subcommand requiredparam --optionalparam1 --optionalparam2=value
Common CLI Commands
There are several CLI commands that are not only rather common but that you should absolutely know to make the best use of this tool.
Install and activate a theme
In this case we install and activate Twenty Twenty-One, the current default theme, which we might not yet have available if our WordPress is a bit outdated:
wp theme install twentytwentyone --activate
Install WordPress
You can use WP-CLI to configure test environments, whose first step is a “vanilla” installation, a standard WordPress without frills:
wp core download
wp core config --dbname=mydbname --dbuser=mydbuser --dbpass=mydbpass --dbhost=localhost --dbprefix=whebfubwef_ --extra-php <<PHP
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
wp db create
wp core install --url=http://siteurl.com --title=SiteTitle --admin_user=username --admin_password=mypassword --admin_email=my@email.com
Let’s see what we have done in detail…
With the first command, the latest version of WordPress is downloaded.
The second command sets the configuration file with database access and some additional PHP lines at the end. In this case, the added constants ensure that our debugging options are active for the test environment.
The third command creates the database (WP-CLI uses the database access information from the configuration file) and finally installs WordPress using a couple of parameters.
Reinstall WordPress core
We can also reinstall the WordPress core using WP-CLI, if for some reason we fear that our installation is corrupted. The following command, in fact, will download the WordPress core without the default themes and plugins.
wp core download --skip-content --force
Change WordPress URL
There are many reasons why you might need to change your WordPress URL. Perhaps you are changing domains, switching to a subdomain, updating from www to non-www, moving files or even migrating from HTTP to HTTPS. In any case, you can easily use the WordPress update options command:
wp option update home 'http://example.com'
wp option update siteurl 'http://example.com'
Get a list of plugins
To obtain a list of plugins currently installed on a site it is sufficient to use the following command: we will see the list, the status (active / inactive), the availability of updates and the current version.
wp plugin list
Install plugins
To install more plugins you can simply “accumulate” parameters one after another. Here’s an example that downloads and activates 3 plugins:
plugin wp installa advanced-custom-fields jetpack ninja-forms --activate
Note that the names of the plugins derive from their name (slug) in the repository. The simplest way to understand this is to visit their page and look at the URL.
If necessary, you can also install previous versions of WordPress plugins with the attribute –version.
wp plugin install wordpress-seo --version=4.8 --activate
Ancillary more interesting: you can install plugins from files on any remote server, not just from the official repository. This is particularly useful if you are developing a plugin or using a premium plugin.
Disable more plugins… or all plugins
To disable a single plugin you can run the following command.
wp plugin deactivate wordpress-seo
To disable all your plugins simultaneously, instead:
wp plugin deactivate --all
This last one can be useful if you are resolving compatibility issues and you simply need to deactivate all plugins at once. You can then go back and enable them one by one, testing what happens as you proceed.
Update plugin
You can also manually update WordPress plugins. For example:
wp plugin update wordpress-seo
Search and replace in database
One of the main reasons why it is difficult to migrate a site simply “copying and pasting” is that the WP database contains serialized arrays, where we have not only the raw data but also the length of that data. So, for example, if you limit yourself to replacing all instances of http://testsite.com with http://livewebsite.com, your serialized arrays will no longer make sense because the character count in strings will not match.
The search-replace command first deserializes the arrays, then performs the search and replace, and finally reserializes everything. This can also be done with a simple command:
wp search-replace vecchia_stringa nuova_stringa
Additional parameters allow you to do much more, including previewing what will be replaced using the –dry-run flag.
It should be noted that the “search-replace” from CLI is often just a first step, as contents created with specific graphic editors – such as BeTheme or Elementor – require specific plugins for URL replacement, as in those cases the data is not simply serialized.
Try Hosting4Agency on a site of your choice for 30 days and compare the performance before/after the trial period!
WP-CLI: Summary
As you may have noticed, the WP-CLI commands are really numerous, so knowing each of them and learning to make full use of them is not something that can be learned overnight.
In Hosting 4 Agency we have accumulated years of daily experience in the use of WP-CLI, to the point that all activation and periodic maintenance scripts we use on our WordPress sites use this technology, allowing us to keep our Managed Hosting infrastructure up-to-date and secure almost automatically (but without giving up the human eye that checks).
Furthermore, we can make WP-CLI available to our customers who have chosen the virtual machine (Virtual Private Server) formula.
Bastará accedere alla propria macchina con una connessione SSH e WP-CLI sarà già disponibile e aggiornata, con la possibilità di ricevere supporto dal team di Hosting 4 Agency.
If you also wish to entrust the WP-CLI tool, as well as all web optimization services, to a team of professionals. Contact us now.
We ensure every day that your websites are always fast, protected and up to date. Get rid of the stress of deadlines and updates, choose Hosting 4 Agency, your partner for WordPress hosting reseller.
Ready to dominate your WordPress site with WP-CLI? Contact us right away and let us help you become the supreme commander of your digital universe!