This tutorial is based on CentOS 7, but many of the commands are applicable to other flavors of Linux.
Getting Started
WP-CLI, as one may gather from the name, is a command line interface for installing and managing instances of WordPress. It is not included with any standard Linux distros, so we will need to install it manually.
[user@localhost ~]$ yum install wp-cli
When using WP-CLI, it must be run either from a directory in which wordpress is already installed, or a directory in which one intends to install wordpress. Additionally one may use a modifier to specify what directory WP-CLI will be working with. Without the modifier WP-CLI will run commands in the current working directory. In the following example we will attempt to check the wordpress core version of a possible wordpress instance in the user home directory using the path modifier.
[user@localhost ~]$ pwd
/home/user
[user@localhost ~]$ wp core version --path='/home/user'
Error: This does not seem to be a WordPress installation.
Pass --path=`path/to/wordpress` or run `wp core download`.
[user@localhost ~]$
Notice the error generated by the fact that no instance of wordpress is present. The error does give us a solution however; it shows us how to download the wordpress core to a given directory. Let’s create a new folder in /var/www and download wordpress there.
[user@localhost ~]$ mkdir /var/www/wordpress-site
[user@localhost ~]$ cd /var/www/wordpress-site
[user@localhost ~]$ wp core download
Downloading WordPress 5.8.2 (en_US)...
md5 hash verified: 1c6bfc773fd0dac60b1fbf6fcbf3599e
Success: WordPress downloaded.
[user@localhost ~]$ ls
index.php wp-blog-header.php wp-includes wp-settings.php
license.txt wp-comments-post.php wp-links-opml.php wp-signup.php
readme.html wp-config-sample.php wp-load.php wp-trackback.php
wp-activate.php wp-content wp-login.php xmlrpc.php
wp-admin wp-cron.php wp-mail.php
[user@localhost wordpress-site]$
Now when we check the WordPress core version, some information will show up:
[user@localhost wordpress-site]$ wp core version
5.8.2
[user@localhost wordpress-site]$
Let’s drill down further into our new WordPress instance. We can look at the list of default plugins with the following command:
[user@localhost wordpress-site]$ wp plugin list
Error: 'wp-config.php' not found.
Either create one manually or use `wp config create`.
[user@localhost wordpress-site]$
Notice that we get an error because this instance of WordPress is not fully set up! If we run the suggested command “wp config create’ without any parameters, the new error will include a list of required parameters.
[user@localhost wordpress-site]$ wp config create
Error: Parameter errors:
missing --dbname parameter (Set the database name.)
missing --dbuser parameter (Set the database user.)
[user@localhost wordpress-site]$
While viewing error messages may not be the most direct way to learn code, seeing and understanding these errors now will make it less shocking if you encounter them in the wild. To move forward we will need to create a database for our new site.
[user@localhost wordpress-site]$ cd /var/lib/mysql
[user@localhost mysql]$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2782
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database wpsite;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> create user 'wpsite'@'localhost' identified by 'p455w0rd';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant all privileges on wpsite.* to 'wpsite'@'localhost';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
[user@localhost mysql]$
Now that we have a database, we can properly create our wp-config file. Notice that we have included additional information such as the database passowrd, the database hostname, the WordPress database prefix, and the locale. While some of these options may contain default values, negating the need for them to be explicitly set, they are included to demonstrate the format of the options. A list of locale codes can be found at https://translate.wordpress.org/.
[user@localhost mysql]$ cd /var/www/wordpress-site
[user@localhost wordpress-site]$ wp config create --dbname=wpsite --dbuser=wpsite --dbpass=p455w0rd --dbhost=localhost --dbprefix=wp_ --locale=en_US
Success: Generated 'wp-config.php' file.
[user@localhost wordpress-site]$
We can view the new file via the less command:
[user@localhost wordpress-site]$ less wp-config.php
<\\?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the
* installation. You don't have to use the web site, you can
* copy this file to "wp-config.php" and fill in the values.
*
* This file contains the following configurations:
*
* * MySQL settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* @link https://codex.wordpress.org/Editing_wp-config.php
*
* @package WordPress
*/
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wpsite' );
/** MySQL database username */
define( 'DB_USER', 'wpsite' );
/** MySQL database password */
define( 'DB_PASSWORD', 'p455w0rd' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
/**
* Authentication Unique Keys and Salts.
*
wp-config.php
We have one more critical task to perform, before WP-CLI will allow us to manage the install. We need to set a site title and an administrator user. If we run the command "wp core install" without any modifiers, we will again get an error with a list of required parameters:
[user@localhost wordpress-site]$ wp core install
Error: Parameter errors:
missing --url parameter (The address of the new site.)
missing --title parameter (The title of the new site.)
missing --admin_user parameter (The name of the admin user.)
missing --admin_email parameter (The email address for the admin user.)
[user@localhost wordpress-site]$
Running the command with properly included parameters yields success:
[user@localhost wordpress-site]$ wp core install --url=http://wordpress-site.com --title=Test --admin_user=admin --admin_password=4dm1np455w0rd --admin_email=admin@host.com --skip-email
Success: WordPress installed successfully.
[user@localhost wordpress-site]$
Plugins
Now that our site is fully set up, let's take a look at the installed plugins.
[user@localhost wordpress-site]$ wp plugin list
+---------+----------+--------+---------+
| name | status | update | version |
+---------+----------+--------+---------+
| akismet | inactive | none | 4.2.1 |
| hello | inactive | none | 1.7.2 |
+---------+----------+--------+---------+
[user@localhost wordpress-site]$
To activate a plugin we can use the following command:
[user@localhost wordpress-site]$ wp plugin activate hello
Plugin 'hello' activated.
Success: Activated 1 of 1 plugins.
[user@localhost wordpress-site]$ wp plugin list
+---------+----------+--------+---------+
| name | status | update | version |
+---------+----------+--------+---------+
| akismet | inactive | none | 4.2.1 |
| hello | active | none | 1.7.2 |
+---------+----------+--------+---------+
[user@localhost wordpress-site]$
To deactivate a plugin we can modify the command as such:
[user@localhost wordpress-site]$ wp plugin deactivate hello
Plugin 'hello' deactivated.
Success: Deactivated 1 of 1 plugins.
[user@localhost wordpress-site]$ wp plugin list
+---------+----------+--------+---------+
| name | status | update | version |
+---------+----------+--------+---------+
| akismet | inactive | none | 4.2.1 |
| hello | inactive | none | 1.7.2 |
+---------+----------+--------+---------+
[user@localhost wordpress-site]$
To remove a plugin use the following form:
[user@localhost wordpress-site]$ wp plugin delete hello
Success: Deleted 1 of 1 plugins.
[user@localhost wordpress-site]$ wp plugin list
+---------+----------+--------+---------+
| name | status | update | version |
+---------+----------+--------+---------+
| akismet | inactive | none | 4.2.1 |
+---------+----------+--------+---------+
[user@localhost wordpress-site]$
Let's now search available plugins for something to install. We can do so with the following query:
[user@localhost wordpress-site]$ wp plugin search elementor
Success: Showing 10 of 1010 plugins.
+----------------------------------------------------------------------+-------------------------------------+--------+
| name | slug | rating |
+----------------------------------------------------------------------+-------------------------------------+--------+
| Elementor Website Builder | elementor | 94 |
| Essential Addons for Elementor | essential-addons-for-elementor-lite | 98 |
| Elementor Header & Footer Builder | header-footer-elementor | 98 |
| Premium Addons for Elementor | premium-addons-for-elementor | 98 |
| ElementsKit Elementor addons (Header & Footer Builder, Mega Menu | elementskit-lite | 96 |
| Builder, Layout Library) | | |
| Happy Addons for Elementor (Mega Menu, Post Grid, Woocommerce Produc | happy-elementor-addons | 96 |
| t Grid, Table, Event Calendar, Slider Elementor Widget) | | |
| Unlimited Elements For Elementor (Free Widgets, Addons, Templates) | unlimited-elements-for-elementor | 96 |
| Livemesh Addons for Elementor | addons-for-elementor | 96 |
| CoDesigner – The Best Elementor Addon to Customize WooCommerce | woolementor | 88 |
| OoohBoi Steroids for Elementor | ooohboi-steroids-for-elementor | 98 |
+----------------------------------------------------------------------+-------------------------------------+--------+
[user@localhost wordpress-site]$
Let's install the main elementor plugin and then activate it.
[user@localhost wordpress-site]$ wp plugin install elementor
Installing Elementor Website Builder (3.5.0)
Downloading installation package from https://downloads.wordpress.org/plugin/elementor.3.5.0.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 1 of 1 plugins.
[user@localhost wordpress-site]$ wp plugin activate elementor
Plugin 'elementor' activated.
Success: Activated 1 of 1 plugins.
[user@localhost wordpress-site]$ wp plugin list
+-----------+----------+--------+---------+
| name | status | update | version |
+-----------+----------+--------+---------+
| akismet | inactive | none | 4.2.1 |
| elementor | active | none | 3.5.0 |
| wordfence | inactive | none | 7.5.7 |
+-----------+----------+--------+---------+
[user@localhost wordpress-site]$
We can view more information about the plugin we just installed using the get modifier:
[user@localhost wordpress-site]$ wp plugin get elementor
+-------------+-------------------------------------------------------------------------------------------------------+
| Field | Value |
+-------------+-------------------------------------------------------------------------------------------------------+
| name | elementor |
| title | Elementor |
| author | Elementor.com |
| version | 3.5.0 |
| description | The Elementor Website Builder has it all: drag and drop page builder, pixel perfect design, mobile re |
| | sponsive editing, and more. Get started now! |
| status | active |
+-------------+-------------------------------------------------------------------------------------------------------+
[user@localhost wordpress-site]$
Themes
The wp theme options can be used in much the same way the wp plugin options. Here we will look up Astra, the current most popular WordPress theme for 2021, install it, and then activate it:
[user@localhost wordpress-site]$ wp theme list
+-----------------+----------+--------+---------+
| name | status | update | version |
+-----------------+----------+--------+---------+
| twentynineteen | inactive | none | 2.1 |
| twentytwenty | inactive | none | 1.8 |
| twentytwentyone | active | none | 1.4 |
+-----------------+----------+--------+---------+
[user@localhost wordpress-site]$ wp theme search astra
Success: Showing 5 of 5 themes.
+------------------+-----------------+--------+
| name | slug | rating |
+------------------+-----------------+--------+
| Astra | astra | 98 |
| Astral | astral | 98 |
| Dark Mode for A. | dark-mode-for-a | 0 |
| PressBook News | pressbook-news | 100 |
| Blush | blush | 20 |
+------------------+-----------------+--------+
[user@localhost wordpress-site]$ wp theme install astra
Installing Astra (3.7.5)
Downloading installation package from https://downloads.wordpress.org/theme/astra.3.7.5.zip...
Unpacking the package...
Installing the theme...
Theme installed successfully.
Success: Installed 1 of 1 themes.
[user@localhost wordpress-site]$ wp theme activate astra
Success: Switched to 'Astra' theme.
[user@localhost wordpress-site]$ wp theme get astra
+----------------+----------------------------------------------------------------------------------------------------+
| Field | Value |
+----------------+----------------------------------------------------------------------------------------------------+
| name | Astra |
| title | Astra |
| version | 3.7.5 |
| status | active |
| parent_theme | |
| template_dir | /var/www/wordpress-site/wp-content/themes/astra |
| stylesheet_dir | /var/www/wordpress-site/wp-content/themes/astra |
| template | astra |
| stylesheet | astra |
| screenshot | screenshot.jpg |
| description | Astra is fast, fully customizable & beautiful WordPress theme suitable for blog, personal port |
| | folio, business website and WooCommerce storefront. It is very lightweight (less than 50KB on fron |
| | tend) and offers unparalleled speed. Built with SEO in mind, Astra comes with Schema.org code inte |
| | grated and is Native AMP ready so search engines will love your site. It offers special features a |
| | nd templates so it works perfectly with all page builders like Elementor, Beaver Builder, Visual C |
| | omposer, SiteOrigin, Divi, etc. Some of the other features: # WooCommerce Ready # Responsive # RTL |
| | & Translation Ready # Extendible with premium addons # Regularly updated # Designed, Develope |
| | d, Maintained & Supported by Brainstorm Force. Looking for a perfect base theme? Look no furth |
| | er. Astra is fast, fully customizable and WooCommerce ready theme that you can use for building an |
| | y kind of website! |
| author | Brainstorm Force |
| tags | ["custom-menu","custom-logo","entertainment","one-column","two-columns","left-sidebar","e-commerce |
| | ","right-sidebar","custom-colors","editor-style","featured-images","full-width-template","microfor |
| | mats","post-formats","rtl-language-support","theme-options","threaded-comments","translation-ready |
| | ","blog"] |
| theme_root | /var/www/wordpress-site/wp-content/themes |
| theme_root_uri | http://wordpress-site.com/wp-content/themes |
+----------------+----------------------------------------------------------------------------------------------------+
[user@localhost wordpress-site]$ wp theme list
+-----------------+----------+--------+---------+
| name | status | update | version |
+-----------------+----------+--------+---------+
| astra | active | none | 3.7.5 |
| twentynineteen | inactive | none | 2.1 |
| twentytwenty | inactive | none | 1.8 |
| twentytwentyone | inactive | none | 1.4 |
+-----------------+----------+--------+---------+
[user@localhost wordpress-site]$
Notice the get modifier can be used with themes in the same way it is used with plugins.
Updates
The following command formats will work for updating the WordPress Core, Plugins, and Themes respectively:
[user@localhost wordpress-site]$ wp core update
Success: WordPress is up to date.
[user@localhost wordpress-site]$ wp plugin update elementor
Success: Plugin already updated.
[user@localhost wordpress-site]$ wp theme update astra
Success: Theme already updated.
[user@localhost wordpress-site]$
To check if there is an available WordPress core update, the check-update option maybe used:
[user@localhost wordpress-site]$ wp core check-update
Success: WordPress is at the latest version.
[user@localhost wordpress-site]$
Working with the Database
The "wp db" command may be used to perform database related tasks. For example the "export" sub-command can be used to save copies of the database as an sql file. After the sub-command the first option is the new file name with full path, which in my case is where I save db backups. The last option indicates the path to the WordPress instance whose database is being backed up.
[user@localhost wordpress-site]$ wp db export /var/lib/mysql/backups/wordpress-site.sql --path='/var/www/wordpress-site/'
Success: Exported to '/var/lib/mysql/backups/wordpress-site.sql'.
[user@localhost wordpress-site]$