The complete guide to master WP CLI : WP CLI Install + Commands

WordPress Command Line Interface

WordPress has gained popularity due to multiple reasons. It is easy to use for every level of user, starting from a beginner to an advanced level user. One such feature of it is it’s ability to be managed using Command Line Interface (CLI).

There are a number of tasks that can be done on a WordPress installation using a Command Line Interface, including taking backups, generating new posts and other administration operations like clearing drafts, commands, core file updates, theme and plugin installations and updates, managingimports and exports, installing a WP site from scratch, troubleshooting issues, managing users on a multi site installation and a lot of other functions that we will go through in detail in this article.

WordPress CLI is believed to be first introduced in 2003 by Daniel Bachhuber, who is an open source expert. Some people believe that it was officially released by Andreas Creten and Cristi Burcă in November, 2011 when it was released on GitHub. Currently it is being managed by Daniel Bachuber. It is a boon for WordPress core developers. WP CLI is installed by server provider for the convenience of doing routine operations.

Some highlights of Command Line Interface:

  • Interact with your WP website using command line.
  • It is Fast and Accurate
  • Provides with end to end WP management options including installations, updation, activation, deactivation and deletion
  • Force reverse updates
  • Option to update individually as well as in bulk
  • Clear all transient data at once, which is not possible from WP Admin Dashboard.
  • It also support WordPress Multisite installations
  • Makes troubleshooting easier.

Latest Version of WP CLI: The current version which is being provided by WPOven by default on all it’s servers, is WP-CLI2, which was launched in August 2018.

Server Requirement to run WP CLI:

Minimum Server Requirement: to run WP-CLI Version 2 PHP 5.4, though the higher the better.

All WPOven servers run PHP 7 and above.

If you yourself manage server and want to install CLI all you need is Linux OS installed, and punch in following commands:

The above 3 commands will perform following action:

  1. It will download the wp-cli.phar file on to the root folder of your web server
  2. Next command will convert it into an executable file
  3. Move wp-cli.phar file to another desired directory where you want to run WP.

In a nutshell, you need the following:

  • Linux OS
  • PHP Version 5.4 or higher.
  • Secure Shell (SSH) access to your hosting server.

To test if WP CLI Installation is correctly configured, enter this command:

It should show a success message like this:

WFUpddJjrn YULVQzNNNtpRYCxfs2tvPMGmjT yNQwAxek6J ORy9JLPRzSpR4nYYsfU8edGZqQDHVoYB8A43q7VsdCWzH5M90fhWzTdef ouzTgmm6Inp3YupFLNdlbOwQBpfS The complete guide to master WP CLI : WP CLI Install + Commands

This is the most basic method to install WordPress Command Line Interface on your Unix server, however there are few tools as well that can do the job for you. Some of them are as follows:

  • Composer
  • Homebrew
  • Docker
  • .deb package
  • Custom default PHP binary
  • MediaTemple on Grid Server

Understanding WP CLI

The importance of WP CLI lies in the fact it improves overall operation speed and offer better control over your WordPress Installation, which minimizes the possibility of error of breakdown of server which often happens in the form of Memory Limit Exceeded, Execution Time Issues, etc.

WP CLI2 also offers an option to do multiple operations using a single commands. This command is known as Bash Script.

For example:

Installing a Plugin and Activating a Plugin are two different functions. While using a WordPress Admin Dashboard you have to first install it and then activate it. Similarly to uninstall, first you have to deactivate it and then delete it.

Using a bash script in WP CLI you can combine the 2 or more functions. So to give you the simplest example, you can install and activate a plugin using a single script.

Using this Bash Script, WP will not only install the plugin but will also activate it.

It will perform following actions in the background:

Similarly, in setting up a wordpress website you have to take a number of actions which includes:

  • Upload and unpack WordPress files on server
  • Create Database
  • Create DB Usernames and passwords
  • Configure wp-config.php file
  • Go through other Installation steps

Using a simple bash script like bash install-and-setup.sh you can easily install a WordPress site using a single script.

There are certain things which are not achievable through WordPress Dashboard (WP ADMIN). For example, clearing all transient data at once. This can be achieved using WP Command Line Interface.

wp transient delete –all

$ wp transient delete –all

Success: 34 transients deleted from the database.

The WP CLI commands classified into two categories:

  1. Set of Predefined Core Internal commands
  2. Community commands which are created by third-party developers.

Hence, it enables you to create your own commands using the pre-defined instructions and syntax to create powerful custom commands, as per developer’s requirement.

How to use WordPress CLIs

Like any other commands, WP CLI also follows a syntax. The basic thing about a command in CLI is it always begins with wp.

Here are some of the popularly used commands:

 

wp menu

List, create, assign, and delete menus.

wp option

Manage options.

wp  plugin

Manage plugins.

wp  post

Manage posts

wp post-type

Manage post types.

wp role

Manage user roles

wp search-replace

Search/replace strings in the database.

wp server

Launch PHP’s built-in web server for this specific WordPress install.

wp sidebar

Manage sidebars

wp site

Perform site-wide operations

wp super-admin

Manage super admins on WordPress multisite

wp taxonomy  

Manage taxonomies.

wp term

Manage terms

wp theme  

Manage themes

wp transient

Manage transients

wp user

Manage users.

wp widget

Manage sidebar widgets.

 

How to use WP CLI to Perform Basic Operations

The very first thing to know about WP CLI operations is that it needs an SSH, also known as Shell access to perform the desired operations.

So, you have to make sure that SSH is enabled on your Server.

How to Enable SSH access for your WPOven server : 

  • Navigate to the ‘Tools’ section.WPOven ssh for cli

 

  • Then from the ‘Tools’ section, you need to press the ‘Enable SSH access’ button at the bottom of the page.wp cli for ssh access

Enable SSH for other hosts via CPanel

The complete guide to master WP CLI : WP CLI Install + Commands

You will have to create login access details for SSH access, as follows:

Cb7YrUz rWjibwFmJhSa1Fg The complete guide to master WP CLI : WP CLI Install + Commands

 

Once the SSH access is enabled for the site, you can then login via 3rd party applications like, Putty or PenguiNet using your site’s SFTP login credentials ( for WPOven users)

The next thing that you need to know is what CLI commands are available to use. To do it go into command prompt and key in wp help in your WordPress site’s root folder. It will display all the commands at your disposal.

To know more details about a particular command you will have to use export in CLI. Example:

It will give you all the detailed support information about the specific user command.

Install WordPress using WP CLI

The latest WP CLI version directly downloads the latest WordPress version from WordPress  website. Following is the sequence of command you need to give in order to have a complete WordPress Installation.

The first step in this series is to download core WordPress Files from WordPress repository on to the server. To do this you have to execute the following command:

This will download latest core files directly from WordPress Server.

If the files are downloaded the command line interface will show a message as follows:

The second step is to setup the database. It can be done using following command.

This command will configure wp-config.php file.

The final step is the WordPress Installation defining website address, username, password and admin email

To update all the WordPress core files anytime, you can use the following command: 

If you want your WordPress Installation to revert to a particular older version you can do that with this command:

Installing and activating WordPress Plugin using WP CLI

The first thing you need to know about installing a plugin using WP CLI is the URL of the plugin in WordPress Repository. So, for example you want to install Classic Editor plugin, you should fetch the root folder of that plugin in WordPress repository, and use it in the command prompt.:

zrT15PGgpi tT1Nh5SoisjKJt4TXMPw5wICdYq97y2yNLE0sXK1BmnlwdvSdpMnh8wc 3hrWdxrU6Gagkfb The complete guide to master WP CLI : WP CLI Install + Commands

 

Once installed you will have to activate plugin by using following command:

Installing Multiple Plugins:

If you want to install and activate multiple WordPress plugins at once, you can mention multiple plugins in the same command. For example:

Similarly, multiple plugins can be deactivated using a similar command

Deactivate all plugins on your WordPress using WP CLI

The plugins can also be updated using a CLI command as shown below:

How to update all plugin using WordPress CLI

Installing a WordPress Theme using WP CLI

You need to execute a simple command to install a theme from WordPress Repository as follows:

To activate the theme you need to execute following command.

Install and activate WordPress theme using WP CLI

Manage your Theme using WP CLI commands :

WP CLI can be used to manage themes on your WordPress Installation. Some of the common operations that you can perform to manage themes include theme identification, download, update and install.

  • Get list of all the current themes on your WordPress site :
  • Search WordPress Repository to find a new theme: 

All the above commands can be combined into a single script as Bash Script as well

Creating New Post on WordPress site using WP CLI:

It is fairly easy to create new posts using WP CLI.

Simply execute the following command.

You can edit a post using the post id in the command as follows:

Where 243 is the post id, simply replace it with the post id which you want to edit. Executing this command will launch post’s current content in the default editor of your server’s Operating System. Of course it will not be a rich text based like WordPress editor.

Search and Replace using WP CLI

There are many plugins in the WordPress repository to search a particular string and then replace it with another but each one of those comes with one or the other drawbacks. Also for large sizes websites it can become a nightmare to use such a plugin to accomplish the goal. The operation often breaks down in between owing to Exceeding of Execution Time limit and Memory Limit. To make sure you search and replace accurately and without breaking down the operation in between you should do it using WordPress Command Line Interface. It is fairly easy to use it and it is pretty fast too.

This is command is particularly useful when you have changed the site’s URL or even added HTTPS to the site’s URL. It is one of the easiest ways to fix Mixed content warning messages on sites with https.

The command that you need to execute is as follows:

The above command won’t make any replacement, it will be executed as a DRY RUN, which means it will just display the expected result once this query is run. It is done as a precautionary step to make sure the result will be as per the expectation. Once you are satisfied you can make actual replacement by removing –dry-run from the above command, as follows:

Using same command you can also replace http:// URLs to https URLs as follows:

Content Migration

One of the other most useful benefit of WP CLI is it simplifies the process to migrate content within WordPress websites as well as migrating content from other open source CMS like Joomla, or Drupal.

It efficiently migrates all the data and information including posts, links, dates, authors, comments, etc, and match to the existing WordPress taxonomies accordingly.

Managing and Creating Users using Command Line Interface:

User management is extremely easy using CLI. You just need to run simple commands and perform user specific operations like list all users, create users, deleting users, etc.

Here are some corresponding commands:

List Users :

Create a new user:

Update existing user

Delete a user and assigning all its post to other user:

WP CLI Change password of existing user

 

There are more User management commands that can be used, as follows:

Name Description
wp user add-cap Adds a capability to a user.
wp user add-role Adds a role for a user.
wp user check-password Checks if a user’s password is valid or not.
wp user create Creates a new user.
wp user delete Deletes one or more users from the current site.
wp user generate Generates some users.
wp user get Gets details about a user.
wp user import-csv Imports users from a CSV file.
wp user list Lists users.
wp user list-caps Lists all capabilities for a user.
wp user meta Adds, updates, deletes, and lists user custom fields.
wp user remove-cap Removes a user’s capability.
wp user remove-role Removes a user’s role.
wp user reset-password Resets the password for one or more users.
wp user session Destroys and lists a user’s sessions.
wp user set-role Sets the user role.
wp user spam Marks one or more users as spam.
wp user term Adds, updates, removes, and lists user terms.
wp user unspam Removes one or more users from spam.
wp user update Updates an existing user.

Managing Comments using WP CLI

Using command line interface a wordpress admin can create, update, delete, and moderate the visitor comments.

Add a Comment using WP CLI

Update Comment:

Delete Comment:

Delete Spam Comments

 

Managing WordPress Multisite using WP CLI

It is an ultimate management tool for WordPress Multisite management and maintenance. Using multisite commands you can creates, deletes, empty, moderate, and list the sites.

Creating a site:

List of All Sites in Multisite using WP CLI:

Delete site in Multisite using WP CLI:

Some other Useful Commands:

Database backup

Update SITE URLs Using WP CLI:

Plugin Status:

Troubleshooting WordPress site using WP CLI

Diagnosing Problem using WP CLI

Sometimes troubleshooting becomes a nightmare for the developers. WP CLI makes it easier though a tool known as WP Doctor, specially in cases where an update is failed or some function breaks up in between.

To use it you will have to install it from git hub.

To see what all this tool can help you with, enter this command:

 

To check whether the installation is working fine, and to cross check all performance issues, you can use this command:

And there is a long list of support options provided in this amazing troubleshooting tool.

Conclusion

WordPress CLI is an open source library of predefined commands to manage Standalone WordPress installation as well Multisite Installations. You can manage themes, plugins, roles, taxonomies, create site in a multisite installs and a lot more, in a command line environment where browser is not used. It is an attempt to provide the fastest interface for WordPress developers to manage their projects.

So, if you have not started using WP CLI yet you are missing out on an important maintenance tool, that will minimize your website downtime while making updates. It will make your life easier as as website manager.

WP CLI allows you to do complete site maintenance, including core and plugin updates, batch processing of media files (images, etc.). It can also be used to manage and update the website structure if you change the theme, design and pages on your WordPress website. For example, you can generate updated sitemap, change the navigation menus, update permalink structure and other taxonomies.

Future of WP CLI lies in utilization of powerful WP REST API through WordPress command line interface. The core team behind WP CLI is working in the direction and is believed to make a success soon.

To have a more detailed knowledge of WP CLI you can visit the official website here. Using CLI you can save a lot of time and server resources, which otherwise is time consuming from front end WordPress Dashboard, and can also break down the operation in between due to one or the other reason. The CLI has improved overall wordpress management experience, by making processes faster and accurate.

 

3 Replies to “The complete guide to master WP CLI : WP CLI Install + Commands”

  1. Nice blog post it’s very useful to me and I get a lot of informations. And I want deep new informations of WordPress website thankyou.

  2. Nice blog post it’s very useful to me and I get a lot of information. And I want deep new information on the WordPress website thankyou.

  3. One of the best intro to wp-cli I’ve seen ! The hints about multisites and WP Doctor were especially useful.

Leave a Reply

Your email address will not be published. Required fields are marked *