Hackernoon logoHow WP-CLI Works: The Command Line Interface For WordPress by@atulhjindall

How WP-CLI Works: The Command Line Interface For WordPress

image
Atul Jindal Hacker Noon profile picture

@atulhjindallAtul Jindal

Design Awesome Websites and Apps using PHP

WordPress is a popular open-source content management system that creates beautiful websites and blogs. WordPress is the most trusted CMS, as almost 64.2 percent of websites are built using it. The ecosystem of WordPress with the large pool of themes and plugins makes it the top choice among website developers who like to build sites with CMS functionality.

WordPress Command Line Interface (CLI)

Its interface is shell-based, allowing developers to perform WordPress administrative tasks that could have been otherwise only possible through the admin dashboard. Wp-admin is a backend web-based user interface to interact with plugins, posts, pages, and themes.

In other words, WP-CLI is a command-line utility that helps developers to perform complex tasks by typing single word commands from the shell and making tasks super fast without even login into the admin panel.

Improving the speed of execution for database backups, files access, theme and plugin management, installation, and other technical management must be considered among WordPress web developers. In a nutshell, using WP-CLI WordPress, developers can work by spending less time on such tasks giving them additional time to work on the other development-related work for WordPress.

Let's dive deeper to understand what WP-CLI is and how it works.

WordPress CLI runs on the command line, and to use WP-CLI, you need to have shell access to your web server. In today most of the hosting providers offer shell or command line access to the hosting account.

For Linux or Mac users, this program is called Terminal, and on Windows, it is called command prompt. On a Windows machine, sometimes, to work with remote servers, you might need to install putty.

WP-CLI installation on WordPress website

You need to make sure your environment is compatible and has the following items in place:

  • Unix style Hosting (Ubuntu, FreeBSD, etc.) with LAMP stack or LEMP stack.
  • PHP 7.0 or later.
  • WordPress 4.2 or later.
  • Server access to the command line.

Firstly, download

wp-cli.phar
using SSH and configure it with the necessary settings. Run the following command to download
wp-cli.phar
in your root directory:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Confirm if the download was successful using

php wp-cli.phar --info

Make the

wp-cli.phar
executable to it can run by simply
wp
command.

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

After execution of all commands above, we can ensure, wp-cli is installed properly by running the command

wp --info
.

In case of successful installation, you will see the currently installed version of WP-CLI on your command prompt. An Alternate way to install CLI is using composer.

Common management tasks using WP-CLI

WordPress CLI, managing your WordPress is at your fingertips, and it has built-in help that can be invoked with

wp help
command, but you can have detailed reference of commands from the official documentation. Here are some of the important commands of
wp-cli
to illustrate how fast certain tasks can be performed.

  • wp core version
    It will return the current version of the WordPress installation.
  • wp core update
    If a newer version is available, it will update the current version of WordPress to the latest version.
  • wp core update --version=4.0 
    If you want to update your WordPress installation to a specific version, you can update it with the command specified above.
    wp theme install twentyten
    It will connect your current WordPress installation with the WordPress theme repository. And install the available themes to your WordPress installation with a matter of issuing one single command. For example, in the above command, it will install theme TwentyTen in your WordPress.
  • wp theme activate twentyten
    This command with activate the installed theme twentyten.
  • wp plugin install woocommerce
    Just like themes, we can install and manage themes with wp-cli. The above command will install a popular woo-commerce plugin to your current WordPress installation.
  • wp plugin activate woocommerce
    To activate a woo-commerce plugin, we can use the above command.
  • wp plugin activate woocommerce
    Similarly, to deactivate the woo-commerce plugin, we use the above command.

Final thoughts

No doubt, WP-CLI is a magical tool to manage WordPress applications, and I would like to add there are many more commands that are available with

wp-cli
, helping developers taking database backups, media backups, manage pages, posts, etc. All these speed up the overall development of WordPress sites and make management easier.

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.