The WordPress Cli (or WP-CLI) is a command line tools for managing WordPress websites through the command line. In this tutorial, you will learn:
- What WP-CLI is,
- how to install WP-Cli using Docker and
- Overview of the basic commands of this tool.
WP-CLI will be particularly useful if you are a WordPress developer, System Administrator or run a business built around WordPress. This command line tool will greatly help you do more in less time. For example, taking backups, updating WordPress and plugins, publishing content and querying databases can be accomplished relatively quickly. For example, you can use this plugin to automatically install Woocommerce in WordPress.
Contents:
Requirements of WP-Cli
- SSH access to your server
- PHP 5.3.2 or later.
- WordPress 3.4 or later.
- UNIX like environment like Linux.
Install WP-Cli
You can either 1) directly install WP-Cli on your system or install it using Docker.
Install directly on your system
# 1/ Download wp-cli
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
# 2/ Make it executable
chmod +x wp-cli.phar
# 3/ Move it into /usr/local/bin/wp
sudo mv wp-cli.phar /usr/local/bin/wp
# Check whether the installation worked
wp --info
Now, you can simply type wp
in your terminal to call WP-Cli. By the way, remember to run wp from the WordPress root directory.
Install WordPress using docker-compose and WP-ClI
See our previous tutorial to automatically install and configure wordpress using docker-compose and WP-CLI.
You can automatically deploy a local docker WordPress site in 5 minutes using the following commands:
# Download a wordpress docker-compose example
git clone https://github.com/kassambara/wordpress-docker-compose
cd wordpress-docker-compose
# Automatic installation of wordpress
make autoinstall
Visit your site at http://localhost and your database via phpMyAdmin at http://localhost:8080.
Default identification for your wordpress website admin:
Username: wordpress
andPassword: wordpress
Default identification for the phpMyAdmin interface:
Username: root
andPassword: password
This installation includes also the WP-ClI tool. In the next sections, we’ll use the docker-compose WP-ClI commands. So, start by creating an alias to simply call the WP-CLI docker-compose commands:
wp="docker-compose run --rm wpcli"
Popular WP-CLI commands
Command | Description |
---|---|
wp cap | Adds, removes, and lists capabilities of a user role. |
wp cli | Reviews current WP-CLI info, checks for updates, or views defined aliases. |
wp comment | Creates, updates, deletes, and moderates comments. |
wp config | Generates and reads the wp-config.php file. |
wp core | Downloads, installs, updates, and manages a WordPress installation. |
wp cron | Tests, runs, and deletes WP-Cron events; manages WP-Cron schedules. |
wp db | Performs basic database operations using credentials stored in wp-config.php. |
wp eval | Executes arbitrary PHP code. |
wp eval-file | Loads and executes a PHP file. |
wp export | Exports WordPress content to a WXR file. |
wp i18n | Provides internationalization tools for WordPress projects. |
wp import | Imports content from a given WXR file. |
wp language | Installs, activates, and manages language packs. |
wp maintenance-mode | Activates, deactivates or checks the status of the maintenance mode of a site. |
wp media | Imports files as attachments, regenerates thumbnails, or lists registered image sizes. |
wp menu | Lists, creates, assigns, and deletes the active theme’s navigation menus. |
wp plugin | Manages plugins, including installs, activations, and updates. |
wp post | Manages posts, content, and meta. |
wp post-type | Retrieves details on the site’s registered post types. |
wp rewrite | Lists or flushes the site’s rewrite rules, updates the permalink structure. |
wp role | Manages user roles, including creating new roles and resetting to defaults. |
wp scaffold | Generates code for post types, taxonomies, plugins, child themes, etc. |
wp search-replace | Searches/replaces strings in the database. |
wp shell | Opens an interactive PHP console for running and testing PHP code. |
wp sidebar | Lists registered sidebars. |
wp site | Creates, deletes, empties, moderates, and lists one or more sites on a multisite installation. |
wp super-admin | Lists, adds, or removes super admin users on a multisite installation. |
wp taxonomy | Retrieves information about registered taxonomies. |
wp term | Manages taxonomy terms and term meta, with create, delete, and list commands. |
wp theme | Manages themes, including installs, activations, and updates. |
wp user | Manages users, along with their roles, capabilities, and meta. |
wp widget | Manages widgets, including adding and moving them within sidebars. |
Update WordPress
# Upgrade to the latest version
$wp core update
# Go back to an older version
wp core update --version=4.9.6 --force
Plugin commands
Name | Description |
---|---|
wp plugin activate | Activates one or more plugins. |
wp plugin deactivate | Deactivates one or more plugins. |
wp plugin delete | Deletes plugin files without deactivating or uninstalling. |
wp plugin get | Gets details about an installed plugin. |
wp plugin install | Installs one or more plugins. |
wp plugin is-active | Checks if a given plugin is active. |
wp plugin is-installed | Checks if a given plugin is installed. |
wp plugin list | Gets a list of plugins. |
wp plugin path | Gets the path to a plugin or to the plugin directory. |
wp plugin search | Searches the WordPress.org plugin directory. |
wp plugin status | Reveals the status of one or all plugins. |
wp plugin toggle | Toggles a plugin’s activation state. |
wp plugin uninstall | Uninstalls one or more plugins. |
wp plugin update | Updates one or more plugins. |
Plugin list
$wp plugin list
+---------+----------+--------+---------+ | name | status | update | version | +---------+----------+--------+---------+ | akismet | inactive | none | 4.1.3 | | hello | inactive | none | 1.7.2 | +---------+----------+--------+---------+
Delete plugin
$wp plugin delete akismet hello
Install and activate plugin
# Install and activate new plugin
$wp plugin install woocommerce --activate
# deactivate a plugin
$wp plugin deactivate woocommerce
# Only activate a plugin
$wp plugin activate woocommerce
# Update plugins
$wp plugin update woocommerce
$wp plugin update --all
If you want to install and activate multiple WordPress plugins at once, you can mention multiple plugins in the same command.
Plugin version and details
$wp plugin activate woocommerce
+-------------+-----------------------------------------------------------------+ | Field | Value | +-------------+-----------------------------------------------------------------+ | name | woocommerce | | title | WooCommerce | | author | Automattic | | version | 3.8.1 | | description | An eCommerce toolkit that helps you sell anything. Beautifully. | | status | active | +-------------+-----------------------------------------------------------------+
Theme commands
Name | Description |
---|---|
wp theme activate | Activates a theme. |
wp theme delete | Deletes one or more themes. |
wp theme disable | Disables a theme on a WordPress multisite install. |
wp theme enable | Enables a theme on a WordPress multisite install. |
wp theme get | Gets details about a theme. |
wp theme install | Installs one or more themes. |
wp theme is-active | Checks if a given theme is active. |
wp theme is-installed | Checks if a given theme is installed. |
wp theme list | Gets a list of themes. |
wp theme mod | Sets, gets, and removes theme mods. |
wp theme path | Gets the path to a theme or to the theme directory. |
wp theme search | Searches the WordPress.org theme directory. |
wp theme status | Reveals the status of one or all themes. |
wp theme update | Updates one or more themes. |
Theme list
$wp theme list
+-----------------+----------+--------+---------+ | name | status | update | version | +-----------------+----------+--------+---------+ | twentynineteen | inactive | none | 1.4 | | twentyseventeen | inactive | none | 2.2 | | twentysixteen | inactive | none | 2.0 | | twentytwenty | active | none | 1.1 | +-----------------+----------+--------+---------+
Activate a theme
$wp theme activate twentyseventeen
Update theme
# Update one theme
$wp theme update twentyseventeen
# Update all theme
$wp theme update --all
Search and replace
This is command is particularly useful when you have changed the site’s URL or even added HTTPS to the site’s URL.
First make a dry run:
$wp search-replace {old URL} {new URL} --dry-run
The above command 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:
$wp search-replace {old URL} {new URL}
Content migration
wp db export
: Exports the database to a file or to STDOUT.wp db import
: Imports a database from a file or from STDIN.
# Exports all the database to a file in wordpress directory
$wp db export db.sql
# Export certain tables
$wp db export --tables=wp_options,wp_users
# Import a database
$wp db import db.sql
It efficiently migrates all the data and information including posts, links, dates, authors, comments, etc, and match to the existing WordPress taxonomies accordingly.
Additional database management commands:
Name | Description |
---|---|
wp db check | Checks the current status of the database. |
wp db clean | Removes all tables with $table_prefix from the database. |
wp db cli | Opens a MySQL console using credentials from wp-config.php |
wp db columns | Displays information about a given table. |
wp db create | Creates a new database. |
wp db drop | Deletes the existing database. |
wp db export | Exports the database to a file or to STDOUT. |
wp db import | Imports a database from a file or from STDIN. |
wp db optimize | Optimizes the database. |
wp db prefix | Displays the database table prefix. |
wp db query | Executes a SQL query against the database. |
wp db repair | Repairs the database. |
wp db reset | Removes all tables from the database. |
wp db search | Finds a string in the database. |
wp db size | Displays the database name and size. |
wp db tables | Lists the database tables. |
Manage users
# List user IDs
$wp user list
# Create a new user.
$wp user create bob bob@example.com --role=author
# Update an existing user.
$wp user update 123 --display_name=Mary --user_pass=marypass
# Delete user 123 and reassign posts to user 567
$ wp user delete 123 --reassign=567
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
Data science
- Course: Machine Learning: Master the Fundamentals by Stanford
- Specialization: Data Science by Johns Hopkins University
- Specialization: Python for Everybody by University of Michigan
- Courses: Build Skills for a Top Job in any Industry by Coursera
- Specialization: Master Machine Learning Fundamentals by University of Washington
- Specialization: Statistics with R by Duke University
- Specialization: Software Development in R by Johns Hopkins University
- Specialization: Genomic Data Science by Johns Hopkins University
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
Trending Courses
- The Science of Well-Being by Yale University
- Google IT Support Professional by Google
- Python for Everybody by University of Michigan
- IBM Data Science Professional Certificate by IBM
- Business Foundations by University of Pennsylvania
- Introduction to Psychology by Yale University
- Excel Skills for Business by Macquarie University
- Psychological First Aid by Johns Hopkins University
- Graphic Design by Cal Arts
Amazon FBA
Amazing Selling Machine
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Version: Français
No Comments