WordPress Command Line Interface
This article covers what the WordPress Command Line Interface utility is, how to read its documentation, and also covers the basic usage of many of its important functions:
- What is WP-CLI and How Do I Use It?
- Reading the WP-CLI Documentation
- Managing Files
- Managing Settings
- Managing Users, Posts, and Comments
Note: WP-CLI is only available via SSH and is not able to be used for installations not using SSH.
What is WP-CLI and How Do I Use It?
The WordPress Command Line Interface (WP-CLI) is a command line utility for managing WordPress. It is available on all Shared, Cloud, and Reseller servers via SSH. The following article will assist with gaining SSH access:
The benefits to using WP-CLI include faster management, easier management of multiple installations, and direct management of installations where the admin dashboard inaccessible.
To manage an installation using WP-CLI, your present workind directory must be the directory to which WordPress is installed. For example, if WordPress is installed to your primary domain, you should execute WP-CLI commands from the /public_html/ directory.
See below for information regarding the use of common commands for WP-CLI:
Reading the WP-CLI Documentation
A full list of WP-CLI's features can be found by running wp help at the command prompt:
From the help documentation page, you can use the up and down arrows to navigate the wp command manual. While reading you may press the q key to exit when done or the h key for a list of commands to use when reading.
Managing Files
The following commands will assist with manipulating files within your WordPress installation including your core files, plugins, and themes:
Core Files
wp core version
This command returns the version of the core files in your WordPress installation. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp core version
4.5
wp core update
This command updates WordPress to the latest version. For security and performance reasons, HostGator recommends always updating to the latest version. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp core update
Success: WordPress is up to date.
wp core update-db
This command will update your WordPress Database. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp core update-db
Success: WordPress database already at latest db version 36686
wp core verify-checksums
This command verifies that your WordPress files match the original file checksums. This command can check for possible malicious compromise. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp core verify-checksums Warning: File doesn't verify against checksum: wp-admin/about.php Error: WordPress install doesn't verify against checksums.
wp core download
This command will download the core files for the most recent version of WordPress into an empty directory. Example usage:
kbwriter@hgunlimited.com [~/public_html/wp]# wp core download
Downloading WordPress 4.5 (en_US)...
Using cached file '/home1/kbwriter/.wp-cli/cache/core/wordpress-4.5-en_US.tar.gz'...
Success: WordPress downloaded.
After performing this command in an empty directory, you will need to continue with step three from the following article regarding how to install WordPress Manually:
Plugins
wp plugin search <search term>
This command will search the WordPress plugins database using the search term entered and provide the slug required to install a plugin using WP-CLI. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp plugin search forum
Success: Showing 10 of 2825 plugins.
+------------------------------+------------------------+--------+
| name | slug | rating |
+------------------------------+------------------------+--------+
| Asgaros Forum | asgaros-forum | 90 |
| WP Forum Server | forum-server | 70 |
| Vanilla Forums | vanilla-forums | 56 |
| Forum Restrict | forum-restrict | 60 |
| Fix Forum Breadcrumbs | fix-forum-breadcrumbs | 0 |
+------------------------------+------------------------+--------+
wp plugin install <plugin-slug>
This command can directly install plugins either from the WordPress plugin database, or from a local path or directory. This example will show how to install a plugin using the slug retrieved by searching the WordPress database using wp plugin search <search term>. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp plugin install forum-server
Installing WP Forum Server (1.8.2)
Downloading install package from https://downloads.wordpress.org/plugin/forum-server.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Translations updates are not needed for the 'English (US)' locale.
For a list of alternate uses, options, and additional parameters, such as to install a plugin from a plugin you have downloaded, please use the command wp help plugin install.
wp plugin [activate|deactivate] <plugin-name>
With the activate and deactivate commands, you can enable or disable installed plugins directly from the command line. Example usages:
kbwriter@hgunlimited.com [~/public_html]# wp plugin activate forum-server
Success: Plugin 'forum-server' activated.
kbwriter@hgunlimited.com [~/public_html]# wp plugin deactivate forum-server
Success: Plugin 'forum-server' deactivated.
You can use the wp plugin list command for a list of installed plugin names.
wp plugin update --all
For security and performance reasons, plugins should always be kept up to date. This command will update all of your plugins to the most recent version. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp plugin update --all
Enabling Maintenance mode...
Downloading update from https://downloads.wordpress.org/plugin/ninja-forms.2.9.42.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Disabling Maintenance mode...
Success: Translations updates are not needed for the 'English (US)' locale.
Success: Updated 1/1 plugins.
+-------------+-------------+-------------+---------+
| name | old_version | new_version | status |
+-------------+-------------+-------------+---------+
| ninja-forms | 2.9.41 | 2.9.42 | Updated |
+-------------+-------------+-------------+---------+
wp plugin delete <plugin-name>
This command will delete a specific plugin based on the plugin name, removing it from your server completely. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp plugin delete forum-server
Success: Deleted 'forum-server' plugin.
You can use the wp plugin list command for a list of installed plugin names.
Themes
wp theme search <search term(s)>
This command will allow you to search the WordPress theme databases, providing slugs for installing themes from the database. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp theme search "twenty sixteen"
Success: Showing 1 of 1 themes.
+----------------+---------------+--------+
| name | slug | rating |
+----------------+---------------+--------+
| Twenty Sixteen | twentysixteen | 76 |
+----------------+---------------+--------+
wp theme install <theme slug>
This command can directly install themes either from the WordPress theme database, or from a local path or directory. This example will show how to install a plugin using the slug retrieved by searching the WordPress database using wp theme search <search term(s)>. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp theme install twentysixteen
Installing Twenty Sixteen (1.2)
Downloading install package from http://downloads.wordpress.org/theme/twentysixteen.1.2.zip...
Unpacking the package...
Installing the theme...
Theme installed successfully.
Success: Translations updates are not needed for the 'English (US)' locale.
For a list of alternate uses, options, and additional parameters, such as to install a theme from a file you have downloaded, please use the command wp help theme install.
wp theme activate <theme-name>
This command will switch the active theme. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp theme activate twentyfifteen
Success: Switched to 'Twenty Fifteen' theme.
For a list of names of installed themes, please use the wp theme list command.
wp theme update --all
HostGator strongly recommends keeping your WordPress, plugins, and theme updated to the latest version to secure your site against malicious users. This command will update all of your installed themes to the most recent version. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp theme update --all
Success: Updated 0/0 themes.
wp theme delete <theme-name>
This command will delete a specific theme based on the theme name, removing it from your server completely. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp theme activate twentyfifteen
Success: Switched to 'Twenty Fifteen' theme.
For a list of names of installed themes, please use the wp theme list command.
Managing Settings
WP-CLI also lets you manage WordPress settings. To view a comprehensive list of options available for managing your WordPress settings, please use the following command:
Other common option updates are as follows:
wp option update [siteurl|home] <value>
These two commands will allow you to fix or update your Home and Site URL directly from the command line, such as if you purchase an SSL and wish to change your protocol to HTTPS. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp option update siteurl https://www.hgunlimited.com
Success: Updated 'siteurl' option.
kbwriter@hgunlimited.com [~/public_html]# wp option update home https://www.hgunlimited.com
Success: Updated 'home' option.
You can use the help command wp help option for a more complete list of the options that may be set with the wp option command.
wp rewrite structure <value>
This command will let you alter the permalinks for your site and the structure they use. In the examples below we will change the permalink structure to include the month and year in every URL, then revert it to only displaying the post name. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp rewrite structure '/%year%/%monthnum%/%postname%'
Success: Rewrite structure set.
kbwriter@hgunlimited.com [~/public_html]# wp rewrite structure '/%postname%'
Success: Rewrite structure set.
Managing Users, Posts, and Comments
It is possible to manage your users from the command line, allowing you to quickly add, delete, and modify users in seconds.
wp user add <username> <user-email>
This command will allow you to add a user to the database, and then will return a password which may be used to log into the WordPress dashboard with. The --role flag may be used to assign a role at creation such as in the example below. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp user create username usersemail@hgunlimited.com --role=author
Success: Created user 4.
Password: REDACTED
wp user set-role <username> <role>
This command will change the role of an existing user. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp user set-role username administrator
Success: Added username (4) to http://www.hgunlimited.com as administrator
You may see a list of existing users with the wp user list command and a list of available roles using the wp role list command.
wp post edit <Post ID>
This command will open a command line text editor to allow you to edit the HTML of the content of the specified post. The post will only update if you save your changes. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp post edit 2
Success: Updated post 2.
You may see a list of existing posts and their IDs with the wp post list command.
wp post delete <Post ID>
This command will delete a specified post. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp post delete 8
Success: Trashed post 8.
You may see a list of existing posts and their IDs with the wp post list command.
You may even moderate your comments directly through the command line:
wp comment approve <comment ID>
This command will approve an existing comment, publishing it to your site. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp comment approve 1
Success: Approved comment 1
You may see a list of existing comments and their IDs with the wp comment list command. You may use the option --force to skip the trash bin and permanently delete the item.
wp comment delete <comment ID>
This command will delete a specified comment based on the provided comment ID. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp comment delete 8
Success: Deleted comment 1.
You may see a list of existing comments and their IDs with the wp comment list command. You may use the option --force to skip the trash bin and permanently delete the item.
wp comment spam <comment ID>
This command will mark the specified comment as spam. Example usage:
kbwriter@hgunlimited.com [~/public_html]# wp comment spam 1
Success: Marked as spam comment 1.
You may see a list of existing comments and their IDs with the wp comment list command. You may use the option --force to skip the trash bin and permanently delete the item.
This article is intended as a beginner guide to the advanced management of WordPress using WP-CLI. Almost every single setting, option, and aspect of WordPress can be managed or changed from the command line using these tools, and the tool is well documented using wp help command for each of the available tools.