Using WP-CLI With Your Managed WordPress Site

Using the Command Line Tools

Liquid Web’s Managed WordPress servers come with a pre-installed set of command-line tools designed to simplify common site maintenance tasks. WP-CLI (WordPress Command Line Interface) can be accessed via SSH, and allows you to do nearly anything that can be done from within the WordPress admin interface.

Among many other things, WP-CLI can be used to:

  • create and modify users and their roles
  • install and activate or deactivate plugins
  • perform basic database maintenance tasks
  • manage cron jobs
  • manage rewrite rules

For the full list of commands, visit the official documentation.

WP-CLI always should be run as the site owner, and always should be run from within the site’s installation directory. For the following examples, I have connected to the server via SSH using the WordPress admin credentials for examplewordpresssite.com, verified my current working directory with the command “pwd”, and then changed directory into the site’s document root with the command “cd”, running “pwd” once again to confirm that I am in the document root.

[examplesiteuser@wphost]# pwd
/home/examplesiteuser/
[examplesiteuser@wphost]# cd public_html
[examplesiteuser@wphost ~/public_html]# pwd
/home/examplesiteuser/public_html

Create a New User and Assign a Role

In this example, we’re going to create a new user named “sample”, assign them the role of Editor on the site, and email them their credentials (a randomly-generated password will be assigned automatically, and the email is sent to their specified address with a link to reset it.):

wp user create sample sample@examplewordpresssite.com --role=editor --send-email

Breaking down this command:

  • wp invokes WP-CLI
  • user create creates the new user
  • sample is the name we chose for the new user
  • sample@examplewordpresssite.com is the new user’s email address
  • –role=editor assigns the role of Editor to the new user
  • –send-email instructs WordPress to email the user’s credentials, and a link to change their password, to the new user’s email address

Install and Activate a Plugin

Here we’re going to download, install, and activate the Meta Slider plugin from wordpress.org.

While we know the name of the plugin, the plugin’s actual file name may be different, so we first will run a search to find out, using the command:

[examplesiteuser@wphost ~/public_html]# wp plugin search metaslider

Running that command tells us that the plugin’s name is “ml-slider”:

[examplesiteuser@wphost ~/public_html]# wp plugin search metaslider
Success: Showing 4 of 4 plugins.
+----------------------------+----------------------------+--------+
| name | slug | rating |
+----------------------------+----------------------------+--------+
| Meta Slider | ml-slider | 96 |
| ThreeWP Broadcast | threewp-broadcast | 94 |
| EWWW Image Optimizer | ewww-image-optimizer | 90 |
| EWWW Image Optimizer Cloud | ewww-image-optimizer-cloud | 90 |
+----------------------------+----------------------------+--------+
[examplesiteuser@wphost ~/public_html]#

Now that we know the file name of the Meta Slider plugin, we can download, install and activate ml-slider with the following command:

wp plugin install ml-slider --activate

Running that code generates the following output:

[examplesiteuser@wphost ~/public_html]# wp plugin install ml-slider --activate
Installing Meta Slider (3.3.6)
Downloading install package from https://downloads.wordpress.org/plugin/ml-slider.3.3.6.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Translations updates are not needed for the 'English (US)' locale.
Activating 'ml-slider'...
Success: Plugin 'ml-slider' activated.
[examplesiteuser@wphost ~/public_html]#

The “Success” messages tell us that ml-slider was installed and activated.

Database Operations

WP-CLI has a number of basic database management features built right in.

Back Up A Database

One particularly useful WP-CLI command allows you to easily back up the WordPress database:

wp db export

Running that command will export the site’s database to an sql file in the current working directory. If you do not specify a name for the file, the exported database file will be the database name with a “.sql” extension, and any existing file with that name will be overwritten. Because we are backing up the database and don’t want this file to overwrite any previous versions, we will be specifying a file name (which must end in .sql).

[examplesiteuser@wphost ~/public_html]# wp db export mydatabase_01042016.sql
Success: Exported to mydatabase_01042016.sql

You always should back up the WordPress database prior to making any significant changes. It takes only a moment and ensures that you will have a restore point in case your changes don’t quite go as planned.

Restore A Database

You can quickly restore a database from an sql file with:

wp db import

Simply supply the file name of the database to restore, and the specified sql file will overwrite the existing database:

[examplesiteuser@wphost ~/public_html]# wp db import mydatabase_01042016.sql
Success: Imported from mydatabase_01042016.sql

Note that importing a database will completely overwrite the existing database. Do not import a database unless you are certain that you have no further use for the existing database or its contents. If in doubt, back up the current version with a unique file name before restoring.

Search and Replace in the Database

WP-CLI includes an advanced search and replace that can be used for delicate operations on database tables. This feature commonly is used to update references to the site name when taking a development site into production.

In this example, we’re going to change the name of the user we created earlier, “sample”, to “example” using the command “wp search-replace”. Note that we’ve already backed up the database above using “wp db export”.

And because we’re changing a username, we’ll also specify that we want only to include the wp_users and wp_usermeta tables in the search and replace, so that the word “sample” doesn’t get replaced with “example” in posts or anywhere else it may crop up:

wp search-replace 'sample' 'example' wp_users wp_usermeta

Breaking down that command:

  • wp invokes WP-CLI
  • search-replace searches for the specified string and replaces it in the specified tables
  • ’sample’ is the string we’re searching for (the old text). The search string needs to be enclosed in quotes.
  • ’example’ is the string we’re replacing the old text with. The string needs to be enclosed in quotes.
  • wp_users wp_usermeta are the specific tables to which we are restricting the search and replace. You can list as many table names as necessary, separating them with a space. If no table name is specified, the search and replace will be performed on the entire database. Performing a search and replace on the entire database is a potentially dangerous operation and should be done only if you are absolutely certain of the results and even then, only after backing up the existing database with a unique name.

 

Note: Do not attempt to run a search and replace (or any other database-altering procedure) without first backing up the database with a unique file name. Even the simplest search and replace has the potential to cause a great deal of damage, or even take the site down (consider the result if we had replaced the word “sample” with “example” in every table in the database for a product sample giveaway site). Always manually take a database backup with a unique file name before running search-replace.

Manage Cron Jobs

WP-CLI can be used to display scheduled cron jobs, run them manually, or even add new ones.

For instance:

wp cron event list

returns a list of all scheduled WordPress crons.

You also may choose to run a listed WordPress cron immediately:

wp cron event run wp_scheduled_delete

executes the wp_scheduled_delete cron event right away, instead of at its normally scheduled time.

Manage Rewrite Rules

WP-CLI gives you the ability to see current rewrite rules as well as change the rewrite structure:

wp rewrite list

displays all current rules.

You also can change the rewrite structure using default WordPress variables. To make your rewrites display the month number first, followed by the year and the name of the post, you would use:

wp rewrite structure '/%monthnum%/%year%/%postname%'

Multisite WordPress Installations

Nearly all the WP-CLI commands can be used the same way on multisite installs as well, so long as you specify the site in the command. This is done by specifying the URL (in the format: –url=domainname.com) in the command.

For example, the command to list scheduled crons for examplewordpresssite.com on multisite would become:

wp cron event list --url=examplewordpresssite.com

Get Involved!

There is a great deal more that can be accomplished with WP-CLI, including the ability to write your own commands and share them with the WP-CLI community.

To get started, check out the Github page and the list of current community commands.
 
 

Be Sociable, Share!
Here's $75, Launch a New VPS Today. Find out why 30,000 customers have chosen our Best-in-Class Performance & 24x7 Heroic Support.