WordPress is one of the content management systems (CMS) used to create and manage websites.WordPress provides a powerful command line interface (CLI) called WP-CLIThe
The WP-CLI is a set of tools for managing WordPress Installationcommand line tool that can be used in a computer terminal or command prompt. With WP-CLI, various tasks can be performed, such asUpdating WordPress, plugins and themes, managing users and roles, and importing and exporting data, without having to leave the terminal.
In this article, learn how to install and use WP-CLI to manage a WordPress site through the terminal, including basic and advanced WP-CLI commands for different tasks.
![图片[1]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107105927767-image.png)
WP-CLI Overview
WP-CLI WP-CLI is an open source project, developed and maintained by a community of developers. It is compatible with most UNIX-like operating systems , including Linux, macOS and Windows. WP-CLI requires PHP 5.6.0 or later , and can be installed as a global package or local dependencies through the Composer package manager .
One of the main advantages of using the WP-CLI is itsSpeed and efficiency. It is often faster to use command line tools than to operate through a graphical user interface (GUI), especially when performing repetitive tasks or automating complex operations.The commands of the WP-CLI can be easily scripted and integrated with other command line tools, which makes it very powerful for developers. (difficult for newbies, just understand)
Advantages of using WP-CLI
Here are some of the main advantages of managing WordPress with WP-CLI:
- Speed and efficiency
WP-CLI can quickly and efficiently perform a variety of actions on a WordPress website without manual intervention. This is useful for managing large or complex websites and automating routine tasks. - Enhance work efficiency
With WP-CLI, you can perform multiple tasks at the same time, saving time and increasing efficiency. This is a boon for developers who manage multiple websites at the same time. - Enhanced control
WP-CLI provides greater control over your website, enabling more efficient management of files, plugins, themes and other components. Useful for developers who need to debug or troubleshoot website issues. - Improved security
WP-CLI makes it easier to manage user accounts, passwords, and other security-related settings, helping to maintain a more secure and stable WordPress installation. - Task automation
WP-CLI is able to automate many common WordPress tasks, such asbacking up, update and optimize. It saves time and also reduces the risk of human error.
![图片[2]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107110014360-image.png)
Requirements for WP-CLI
To install WP-CLI, you need to have a supporting SSH Access The hosting (AliCloud,Tencent cloud(Or any random one from Amazon Cloud).
Almost all popular WordPress hosts support SSH access to the server. Before installing WP-CLI, the environment should meet the following minimum requirements:
- PHP: WP-CLI requires PHP 5.6.0 or higher, PHP 7 or higher is recommended.
- WordPress: WP-CLI for WordPress 3.7 or higher. (Of course they are now updated toWordPress 6.7 (I've got it.)
- operating system: WP-CLI is compatible with Linux, macOS and Windows operating systems. However, WP-CLI has limited support in Windows environment.
These are the basic requirements for installing and using WP-CLI. Specific requirements may vary depending on the plugins and themes used on the site.
How to install WP-CLI
Here are the simple steps to install WP-CLI:
Connect to the server root via SSH
To access the server with SSH, ask for the following information:
- Administrator username and password
- Server IP Address
- port number
Then you need to create a Key Pair. Depending on the operating system, refer to the tools and tutorials below:
- Windows (computer): using PuTTY
- Linux: Using Ubuntu's own terminal
- Mac: with Termius
this paper is based on PuTTY as an example, but you can choose other terminal emulators as well.
In PuTTY:
- show (a ticket) Sessions Page.
- importationHost Name or IP Address, and the port number (Port).
- Select the connection type as SSHThe
- Click on the bottom of the Open Button.
![图片[3]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107100836233-image.png)
After connecting to the server, you will be prompted to enter your username and password, which can be found in theServer DetailsFound in.
![图片[4]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107100905285-image.png)
How to use WP-CLI
After installing WP-CLI on your computer, you can open a terminal window and type "wp", then enter the command and its parameters to run the different WP-CLI commands.
The following are the common management functions of WP-CLI:
- Managing WordPress Manage your WordPress site with WP-CLI, including updates, settings and actions.
- Managing WordPress Themes Install, activate, update or remove themes with WP-CLI.
- Managing WordPress Plugins Install, enable, disable, or update plug-ins with WP-CLI.
- Managing WordPress Core Use WP-CLI to update WordPress core files, install a specific version, or check version information.
- Managing WordPress Multisite Manage a WordPress multisite network with WP-CLI, including creating new sites or managing existing ones.
- Search or replace strings Search or replace strings in the database with WP-CLI, great for changing URLs or updating content in bulk.
- List all supported commands Use the WP-CLI to view all supported commands, enter the following command for a complete list:
wp help
1. Managing WordPress with WP-CLI
The next step is to download and configure WordPress via the WP-CLI command.
Download WordPress
The following command is to download the latest version of WordPress core files:
wp core download
Create the wp-config file
Generate it with the following command wp-config.php
file, which contains the main configuration settings for your WordPress website:
wp config create --dbname=testing --dbuser=wp --dbpass=securepswd --locale=ro_RO
Sample Database Configuration::
- Database Name:
testing
- Database username:
wp
- Database password:
securepswd
Example of Command Options
The previously mentioned command is just a variant of the WP-CLI functionality that generates a standard version of the wp-config.php
Documentation.
The following is a variant of the command that will wp-config.php
Papersstart using WP_DEBUG
respond in singing WP_DEBUG_LOG
::
# Enable WP_DEBUG and WP_DEBUG_LOG
wp config create --dbname=testing --dbuser=wp --dbpass=securepswd --extra-php <<PHP
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
WP_DEBUG_LOG
#Success: Generated 'wp-config.php' file.
Success Tip::#Success: Generated 'wp-config.php' file.
Preventing security breaches
Administrator user credentials may be stored in the bash
history. which can pose a serious security risk. the WP-CLI provides a command variant that addresses this problem:
# Avoid disclosing password to bash history by reading from password.txt
# Using --prompt=dbpass will prompt for the 'dbpass' argument
$ wp config create --dbname=testing --dbuser=wp --prompt=dbpass < password.txt
#Success: Generated 'wp-config.php' file.
Creating a database
Use the following command to create the database. Note that this line of command creates a database based on the wp-config.php
The name of the database used in the fileCreate a new database::
wp db create
![图片[5]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107102320191-image.png)
Installing WordPress
Installing WordPress on your server from the command line via WP-CLI is very simple. The command requires the following parameters:
- URL: Website address
- Title: Website Title
- Admin Username: Administrator username
- Password: Administrator password
- Admin Email: Administrator's mailbox
The following is a sample installation command:
wp core install --url=your_domain --title=Your_Blog_Title --admin_user=username --admin_password=password --admin_email=your_email.com
![图片[6]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107102503208-image.png)
Success Tip: Congratulations! Your WordPress site has been successfully set up.
2. Managing WordPress Themes with WP-CLI
With WP-CLI, you can connect your server directly to the WordPress Theme Library and import themes quickly. Installation and activation of WordPress themes with WP-CLI is very simple, as well as updating and deleting themes with WP-CLI.
WP-CLI wp theme
command combines options and parameters that can be used to handle all topic management tasks.
Install Theme
Install the theme with the following command. For example, install the Twenty Twenty Two theme:
wp theme install twentytwentytwo
take note of: Ensure that the theme name is used in the same way as the name in the page URL. For example, the parameter used here is twentytwentytwo
The
![图片[7]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107103655122-image.png)
Activate the theme
After the installation is complete, run the following command to activate the theme:
wp theme activate twentytwentytwo
Once run, the WordPress website theme will change to Twenty Twenty Two.
![图片[8]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107103706797-image.png)
3. Managing WordPress Plugins with WP-CLI
Installing WordPress plugins via WP-CLI is almost instantaneous as there is no need to click a button or login to the WordPress admin panel to install the plugin.
Installation of plug-ins
Run the following command to install the WordPress plugin:
wp plugin install [plugin name]
take note of: Will [plugin name]
Replace with the plugin name, which should match the name in the page URL. For example, to install the Contact Form 7 plug-in that uses the contact-form-7
as a command parameter.
![图片[9]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107103746133-image.png)
Installing WooCommerce
Here is the installation WooCommerce Example of a plugin which is an e-commerce plugin:
wp plugin install woocommerce
Within seconds the plugin is installed. After installation, run the following command to activate the plugin:
wp plugin activate woocommerce
![图片[10]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107103856482-image.png)
4. Managing WordPress core with WP-CLI
Check WordPress core version
Run the following command to check the current version of WordPress core:
wp core version
![图片[11]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107104034796-image.png)
command returns the current WordPress kernel version number.
Update WordPress core files
Run the following command to update the WordPress core files to the latest version:
wp core update
![图片[12]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107104241126-image.png)
Once executed, the WordPress core will be updated to the latest available version on WordPress.org.
5. Manage WordPress Multisite with WP-CLI
For web agencies with many clients, WP-CLI is the fastest way to update all sites in a WordPress multisite.
Running WP-CLI Commands in WordPress Multisite
When running the WP-CLI command in WordPress Multisite, you must pass the --url
parameter specifies a specific website URL on the network. for example:
wp theme status twentytwentytwo --url=yourwebsite.com
The above command checks the yourwebsite.com posted on Twenty Twenty Two State of the Subject.
![图片[13]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107104836253-image.png)
6. Search or replace strings with WP-CLI
When migrating a website to a new server, many people will run into the problem of old URLs needing to be replaced. This can be solved by using WP-CLI's search and replace command.
Search and replace command
Run the following command for search and replace:
wp search-replace https://oldsite.com https://newsite.com
7. List all commands supported by WP-CLI.
To get started with WP-CLI, simply type wp
Then press enter and the system will list all the supported commands for managing WordPress via WP-CLI.
View details of the command and its options
Run the following commands for more detailed information about a command and its options:
wp help
For example, to get detailed information about plug-in commands, you can run the following command:
wp help plugin
![图片[14]-WP-CLI 命令指南:快速高效管理 WordPress 网站的终极工具](https://www.361sale.com/wp-content/uploads/2025/01/20250107105250989-image.png)
WP-CLI Command List
The following are some common WP-CLI commands and their functions:
command | functionality |
---|---|
wp help | View details of commands and options |
wp cli version | Check the version of WP-CLI |
php wp-cli.phar --info | make executable |
wp core download | Download the latest version of WordPress core files |
wp config create | Creating Configuration Files |
wp db create | Creating a new database |
wp theme install | Install Theme |
wp theme activate | Activate the theme |
wp theme deactivate | Deactivate Theme |
wp plugin install | Installation of plug-ins |
wp plugin activate | Activation Plugin |
wp search-replace | Search/replace strings in the database |
wp help plugin | Viewing details of plug-in commands |
WP-CLI provides more commands to operate your WordPress site according to your needs. A complete list of commands and instructions for using them can be found in the official document Found in.
summarize
WP-CLI provides a rich set of commands that allow users to easily install, activate, deactivate, update and manage WordPress plugins, themes and core files. It can also be used to manage posts, users and WordPress databases, making it a powerful WordPress administration tool.
Frequently Asked Questions
Q: How do I run WP-CLI commands on WordPress?
Follow these steps to run the WP-CLI command:
- Connect to the server's root directory via SSH;
- Download WP-CLI on the server;
- Install WordPress and start using WP-CLI.
Q: In which directory can I install WordPress?
It is recommended to install WordPress in the root directory. In Cloudways, we install it in the public_html folder.
Q: How can I view users via WP-CLI?
Use the following ah command to view the list of users:
wp user list --field=ID
Q: How can I activate or deactivate a plugin directly through WP-CLI?
Installation of plug-ins: Install the plugin with the following command:
wp plugin install woocommerce
Activation Plugin: Activate the plugin with the following command:
wp plugin activate woocommerce
Deactivating plug-ins: Deactivate the plugin with the following command:
wp plugin deactivate woocommerce
Link to this article:https://www.361sale.com/en/32508The article is copyrighted and must be reproduced with attribution.
No comments