WP-CLI stands for WordPress Command Line Interface and it will allow you to interact with your WordPress website running a terminal styled environment for faster development.
You can use one line of code to install and deploy WordPress, you can install and activate plugins, alter, move and manipulate it with ease using just command line.
WP-CLI usage
WP-CLI is useful for speeding up your entire development process. It allows you to quickly set up WordPress installation, installs any desired plugin and performs altering of files quickly. Lots of developers are already used to tools that are using a terminal style of access and therefore it will fit easily into their development routines.
On the other hand, there are many WordPress developers that are not using any kind of similar tools, so they might fear of complexity behind, but I can’t tell you that it’s not so complicated as it might look.
WP-CLI Installation
The recommended way to install WP-CLI is by downloading the Phar build (archives similar to Java JAR files, see this article for more detail), marking it executable, and placing it on your PATH.
First, download wp-cli.phar using wget
or curl
. For example:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Then, check if it works:
php wp-cli.phar --info
To be able to type just wp
, instead of php wp-cli.phar
, you need to make the file executable and move it to somewhere in your PATH. For example:
chmod +x wp-cli.phar sudo mv wp-cli.phar /usr/local/bin/wp
Now try running wp --info
. If WP-CLI is installed successfully, you’ll see output like this:
PHP binary: /usr/bin/php5 PHP version: 5.5.9-1 ubuntu4.14 php.ini used: /etc/php5/cli/php.ini WP-CLI root dir: /home/wp-cli/.wp-cli WP-CLI packages dir: /home/wp-cli/.wp-cli/packages/ WP-CLI global config: /home/wp-cli/.wp-cli/config.yml WP-CLI project config: WP-CLI version: 0.23.0
The installation should be fully completed now. :)
Updating WP-CLI
If you have installed WP-CLI using the recommended Phar method, you can update it at any time by running wp cli update
(although if WP-CLI is owned by root, that may be sudo wp cli update
). If you installed WP-CLI using the Composer or Git-based installations, see the specific instructions for updating associated with each method below.
When you run wp cli update
, you’ll be prompted to confirm that you wish to update with a message similar to the following:
You have version 0.21.1. Would you like to update to 0.23.1? [y/n]
After you accept, you should see a success message:
Success: Updated WP-CLI to 0.23.1
If you’re already running the latest version of WP-CLI, you’ll see this message:
WP-CLI is at the latest version.
Want to live life on the edge? Run wp cli update --nightly
to use the latest nightly build of WP-CLI. The nightly build is more or less stable enough for you to use in your local environment and always includes the latest and greatest.
Basic WP-CLI Commands
WP-CLI offers a various set of commands and we will review only some common ones. You can see the entire WP-CLI commands list
Using the WP-CLI Help System
WP-CLI comes with a classic terminal styled help system. You can use it in the format “wp help”, for example:
wp help cache
Installing WordPress with WP-CLI
One of the main features of WP-CLI is the option to Install WordPress using WP-CLI command wp core install
. Along with that command, we need to pass parameters such as website URL, Blog title, Admin Username, Password, and Admin Email at the end.
wp core install --url="your_website" --title="Blog Title" --admin_user="admin username" --admin_password="enter_your_password" --admin_email="enter_your_email"
Installing Themes with WP-CLI
You can install Themes very quicky through WP-CLI than going into WordPress admin, searching and then activating it.
It will connect your website to WordPress theme repository and import the theme in a matter of a few seconds. For example, to install a theme like TwentyTen, we will use the following command:
wp theme install twentyten
Similarly, to install TwentyTen theme, our command will become:
wp theme activate twentyten
Installing Plugins with WP-CLI
You can also install any plugin from official WordPress plugin repository. Installation is seamless and takes almost no time.
For example: wp plugin install jetpack
The above command will install the JetPack plugin on your website. To activate it, we will use the command:
wp plugin activate jetpack
Similarly, to deactivate any plugin, the command becomes:
wp plugin deactivate jetpack
Updating WordPress Core, Themes and Plugins with WP-CLI
To update the WordPress core to the latest stable version of WordPress, use this command:
wp core update
Update your WordPress installation to the desired version, in this example 4.7.5:
wp core update --version=4.7.5
You can also revert back to some older version if really needed:
wp core update --version=4.2 --force
You can also run the update for some specific plugin, just using one line of code or update all at once. See here:
wp plugin update jetpack
wp plugin update --all
You can update WordPress themes in similar way, just use the following commands:
wp theme update twentyten
wp theme update --all
Conclusion
So, the WP-CLI is definitely a useful tools for making your WordPress development and maintenance process faster and scalable. You can use WP-CLI in more way in order to automate many things and enjoy the benefits of quick and easy access.
Feel free to share your opinion on WP-CLI here, or ask just about any question related to it in the comments section.