Manage WordPress from the command line

wordpress

Tired to manage your sites from the browser, you feel more comfortable using the command line then WP-CLI is for you.

WP-CLI is a set of tools that allows you to manage your WordPress sites without using the browser. With WP-CLI you can:

+ Install / upgrade WordPress.
+ Install / update/enable/disable plug-ins.
+ Install / update / activate / deactivate themes.
+ Manage users.
+ Perform simple DB operations.
+ Manage comments.
+ Manage WordPress multi-site installations.
+ Manage WordPress options and more

Requirements

+ UNIX Environment (GNU/Linux, *BDS, Cygwin)
+ PHP 5.3.2 or higher
+ WordPress 3.5.2 or higher
+ MySQL or MariaDB
+ Apache, NGINX or compatible PHP Web server

Install WP-CLI

You can install WP-CLI using curl or wget, in this case we use curl.

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

Check that it works

$ php wp-cli.phar --info

It should show something like this:

PHP binary: /usr/bin/php5
PHP version: 5.6.13-0+deb8u1
php.ini used: /etc/php5/cli/php.ini
WP-CLI root dir: phar://wp-cli.phar
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 0.20.2

For easier use we set exec perms and move it to /usr/local/bin

$ chmod a+x wp-cli.phar && sudo mv -v wp-cli.phar /usr/local/bin/wp

See the available command

$ wp --help
NAME

wp

DESCRIPTION

Manage WordPress through the command-line.

SYNOPSIS

wp 

SUBCOMMANDS

cache Manage the object cache.
cap Manage user capabilities.
cli Get information about WP-CLI itself.
comment Manage comments.
core Download, install, update and otherwise manage WordPress proper.
...

See the documentation of a command

For example to see the documentation of the command core

$ wp core --help
NAME

wp core

DESCRIPTION

Download, install, update and otherwise manage WordPress proper.

SYNOPSIS

wp core 

SUBCOMMANDS

check-update Check for update via Version Check API. Returns latest version if there's an update, or empty if no update available.
config Generate a wp-config.php file.
download Download core WordPress files.
...

Download WordPress

The following command assumes that:

-The /var/www/wplab DIR exists
-The user under which runs your web server is www-data
-Your web server has write permissions in /var/www/wplab
-The selected language is Spanish.

$ sudo -u www-data wp core download --path=/var/www/wplab --locale=es_ES

We create the WordPress configuration file

$ sudo -u www-data cp -v /var/www/wplab/wp-config-sample.php /var/www/wplab/wp-config.php

We set the desired values

You can edit the configuration file by running the following command

$ sudo -u www-data vim /var/www/wplab/wp-config.php

Look for the following parameters and set the desired values

/ * The name of your WordPress database * /
define('DB_NAME', 'nombredetubasededatos');

/ * Your MySQL user name * /
define('DB_USER', 'nombredeusuario');

/ * Your MySQL password * /
define('DB_PASSWORD', 'contraseña');

/ * MySQL Host (it is very likely that you don't need to change it) * /
define('DB_HOST', 'localhost');

/**
* The WordPress database prefix.
*
You change the prefix if you want to install multiple blogs on a single database.
* It uses only numbers, letters, and underscore.
*/
$table_prefix = 'wp_';

Once we set the above values proceed to the

Installation of WordPress

Before proceeding with the installation, you must have a web server installed and configured, having created the database specified in the parameter DB_NAME and DB_USER user must have permissions to create tables in the database. For more information, please see the article: Install and configure Apache HTTP server, MySQL server administration – Basic and MySQL user administration. Then, run the following command:

$ sudo sudo -u www-data wp core install --path=/var/www/wplab/ --url=wplab.local --title='WP Lab Test' --admin_user=test --admin_password=test123 --admin_email=test@email.com

Set the values of the parameters according to your needs.

Administration of plug-ins

List

$ sudo -u www-data wp plugin list --path=/var/www/wplab/
+-----------------+----------+--------+---------+
| name | status | update | version |
+-----------------+----------+--------+---------+
| akismet | inactive | none | 3.1.5 |
| hello | inactive | none | 1.6 |
| autodescription | inactive | none | 2.3.3 |
+-----------------+----------+--------+---------+

Find

For example, if you want to search all plug-ins that contain the word seo

$ sudo -u www-data wp plugin search seo --path=/var/www/wplab/
Success: Showing 10 of 1722 plugins.
+-------------------------------+-------------------------------+--------+
| name | slug | rating |
+-------------------------------+-------------------------------+--------+
| SEO | seo-wizard | 72 |
| SEO Ultimate | seo-ultimate | 78 |
| WordPress SEO Comments Plugin | blog-comments-seo | 86 |
| Yoast SEO | wordpress-seo | 90 |
| SEO by SQUIRRLY™ | squirrly-seo | 86 |
| SEO Post Content Links | content-links | 100 |
| SEO Plugin LiveOptim | liveoptim | 94 |
| WP Meta SEO | wp-meta-seo | 100 |
| Stallion WordPress SEO Plugin | stallion-wordpress-seo-plugin | 82 |
| Lazy SEO | lazy-seo | 72 |
+-------------------------------+-------------------------------+--------+

The above command only shows 10 plug-ins if you want show 100 instead of 10 we would do in this way.

$ sudo -u www-data wp plugin search seo --per-page=100 --path=/var/www/wplab/
Success: Showing 100 of 1722 plugins.
+---------------------------------+--------------------------------+--------+
| name | slug | rating |
+---------------------------------+--------------------------------+--------+
| SEO | seo-wizard | 72 |
| SEO Ultimate | seo-ultimate | 78 |
| WordPress SEO Comments Plugin | blog-comments-seo | 86 |
| Yoast SEO | wordpress-seo | 90 |
| SEO by SQUIRRLY™ | squirrly-seo | 86 |
| SEO Post Content Links | content-links | 100 |
| SEO Plugin LiveOptim | liveoptim | 94 |
| WP Meta SEO | wp-meta-seo | 100 |
| Stallion WordPress SEO Plugin | stallion-wordpress-seo-plugin | 82 |
| Lazy SEO | lazy-seo | 72 |
| Bubble SEO | bubble-seo | 84 |
...

WP-CLI does not sort the result by rating so here is a trick that allows to sort the result and discard the lines that do not conform up a valid CSV. Before you run the following commands you must have installed the cvstool tool. The sequence is as follows:

1. First finds plug-ins and the result is passed to the AWK tool.
2. AWK checks if it’s the first or second line (headlines) prints the result and passes to the next line.
3. If the line is the 3rd or higher then AWK check that fields 2 (slug) and 3 (rating) are not empty.
4. then AWK check if field 1 (name) end with double quote (“) if it begins with double quote.
5. If not match 3 and 4 then moves to the next line.
6. AWK passes to the sort command the remaining lines.
7. sort sorts lines by the value in the field 3 (rating).
8. The result is passed to the command csvtool to print it in understandable format.

$ sudo -u www-data wp plugin search seo --path=/var/www/wplab/ --per-page=100 --format=csv | awk -F, 'NR

Another solution would be to use csvkit and we simplify the above command to

$ sudo -u www-data wp plugin search seo --path=/var/www/wplab/ --per-page=100 --format=csv --quiet|csvsort -r -c rating|csvlook

|-------------------------------------------------------------+------------------------------------------+---------|
| name | slug | rating |
|-------------------------------------------------------------+------------------------------------------+---------|
| Link to us - SEO Widget | link-to-us | 100 |
| The SEO Framework | autodescription | 100 |
| SEO Post Content Links | content-links | 100 |
| SO Hide SEO Bloat | so-clean-up-wp-seo | 100 |
| Greg's High Performance SEO | gregs-high-performance-seo | 100 |
| RankScanner: SEO Audit | rankscanner-seo-audit | 100 |
| Sewn In Simple SEO | sewn-in-simple-seo | 100 |
| Auto Clean URL for SEO | auto-clean-url-seo | 100 |
| SEO Enforcer for Yoast SEO | seo-enforcer | 100 |
| Expand SEO | best-seo | 100 |
...

Install

Now that we have the list of plugins we select one and installed it for example: The SEO Framework.

$ sudo -u www-data wp plugin install autodescription --path=/var/www/wplab/

Note that autodescription is the slug of the complement

Update

$ sudo -u www-data wp plugin update autodescription --path=/var/www/wplab/

You can update all the plugins using –all option.

Activate

$ sudo -u www-data wp plugin activate autodescription --path=/var/www/wplab/

Disable

$ sudo -u www-data wp plugin deactivate autodescription --path=/var/www/wplab/

Delete

$ sudo -u www-data wp plugin delete autodescription --path=/var/www/wplab/

Note: The theme management is similar to the plugins management.

User management

List

$ sudo -u www-data wp user list --path=/var/www/wplab/
+----+------------+--------------+----------------+---------------------+---------------+
| ID | user_login | display_name | user_email | user_registered | roles |
+----+------------+--------------+----------------+---------------------+---------------+
| 1 | test | test | test@email.com | 2015-10-27 04:03:51 | administrator |
+----+------------+--------------+----------------+---------------------+---------------+

Create

$ wp user create librebyte info@lb.com --role=author --path=/var/www/wplab/
Success: Created user 2.
Password: b3JCP0W7upQ1

Get information from a user

$ wp user get librebyte --path=/var/www/wplab/
+-----------------+---------------------+
| Field | Value |
+-----------------+---------------------+
| ID | 2 |
| user_login | librebyte |
| user_email | info@lb.com |
| user_registered | 2015-10-30 22:06:47 |
| display_name | librebyte |
| roles | author |
+-----------------+---------------------+

Change a user’s password

$ wp user update --user_pass=nueva-contraseña sedlav --path=/var/www/wplab/
Success: Updated user 1.

Delete

wp user delete librebyte --path=/var/www/wplab/
--reassign parameter not passed. All associated posts will be deleted. Proceed? [y/n]
Success: Removed user 2 from http://wplab.local

Manage DB

All commands related to the administration of DB use values from the configuration file.

Open MySQL console

$ wp db cli --path=/var/www/wplab/
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 190
Server version: 5.5.46-0+deb8u1 (Debian)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql>

List tables

$ wp db tables --path=/var/www/wplab/
wp_users
wp_usermeta
wp_posts
...

Execute query

$ wp db query 'desc wp_posts' --path=/var/www/wplab/
+-----------------------+---------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------------+------+-----+---------------------+----------------+
| ID | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| post_author | bigint(20) unsigned | NO | MUL | 0 | |
| post_date | datetime | NO | | 0000-00-00 00:00:00 | |
...

Make backup

$ wp db export wplab.sql --path=/var/www/wplab/
Success: Exported to wplab.sql

Other subcommands:

+ create: creates the DB
+ drop: removes the DB
+ import: import from a sql file
+ optimize: optimize DB
+ repair: repair the database
+ reset: removes the tables from DB

Further readings

Proyecto WP-CLI

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.