The traditional approach to managing a WordPress website is simple. You log in to the WordPress admin panel using a web browser and make changes to your website.

But this approach doesn’t work for users that管理多个WordPress网站。It can be tedious to perform administrative tasks while switching between multiple tabs and windows and managing multipleaccess credentialsall at once.

Luckily, there’s a solution to this problem. It’s called WP-CLI, aka the WordPress Command-Line interface. 使用HubSpot的WordPress网站来发展业务

WP-CLI是一种开源工具,可让您几乎可以使用命令行接口在WordPress管理员中执行任何任务。而且,这还不是全部;它甚至使您可以为您的网站独特的任务创建自定义命令。

Curious to learn more? In this post, we’ll review everything you need to know about working with WP-CLI, including what it is, how to install it, and how to use it with your WordPress websites.

What is WP-CLI?

WP-CLI is an open-source tool that allows WordPress users to perform website administration tasks using a command-line interface. It serves as an alternative to the WordPress admin and even allows you to interact with your website remotely.

It’s worth pointing out that WP-CLI isn’t aWordPress plugin。它是包装在PHAR中的PHP代码(即PHP档案),它使其易于分发应用程序。因此,您可以通过将其安装在UNIX型操作系统上,而无需WordPress网站。

How to Install WP-CLI

您可以以多种方式安装WP-CLI,例如使用git,Composer, andDocker。However, the recommended installation method is using the Phar build.

Here’s how to install the latest WP-CLI version on your server:

Step 1: Check your prerequisites.

Before you install WP-CLI, make sure your server environment consists of:

  1. 基于UNIX的操作系统
  2. WordPress版本3.7+
  3. PHP版本5.6或更高版本

如果确实如此,请继续下一步。如果没有,您可能必须进行一些挖掘,以确定您的设置是否与WP-CLI兼容。

Step 2: Download the WP-CLI Phar file.

Download the Phar file from theWP-CLI repousing eitherwgetorcurl。这是如何使用该文件的方法curlcommand:


 

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


而且,这就是您的WordPress网站的外观:

phar file wordpress

Step 3: Verify the Phar file.

在安装WP-CLI之前,请确保文件通过使用以下命令来工作:


 

$ php wp-cli.phar- info


如果你看到一个错误,删除文件使用rm命令,验证您已正确复制并粘贴下载命令,然后再次下载文件。

Step 4: Make the file executable.

To start using WP-CLI, you need to make the file “executable.” You can do this using the following command:


 

$ chmod +x wp-cli.phar


船尾er this, you can start executing WP-CLI commands by typingphpwp-cli.phar其次是命令。例如:


 

$ php wp-cli.phar cli update


Step 5: Move the file to your PATH.

Finally, to make it easy to access the WP-CLI, we’ll move the executable file to a folder in our PATH so we can call it from anywhere on our system using only WP


 

$ sudo mv wp-cli.phar /usr/local/bin/wp


船尾er you’re done installing WP-CLI, you can start using it immediately. If you’re not sure where to start, it helps to understand the command syntax.

Basics of the WP-CLI Command Syntax

WP-CLI基本语法

WP-CLI commands make it easy for users to perform complex tasks using precise instructions. They eliminate the need to interact with a browser and speed up the website administration process.

但是,不可能通过一个应用程序解决所有可能的用例。因此,WP-CLI仅提供执行与WordPress管理相关的管理任务所需的命令。

Still, you can create custom commands for WP-CLI using itsinternal API— and that’s not all.WordPress developerscan also create custom WP-CLI commands for plugins and bundle them within the plugin code or distribute them as standalone packages. A WP-CLI command usually consists of the primary command, a positional argument, and an associative argument.

假设您正在尝试通过CLI在网站上安装插件,可以使用以下命令语法:


 

$ wp plugin install ... [--version=] [--force] [--activate] [--activate-network]


In the above example,wp plugin installis the primary command,<plugin|zip|url>...is the positional argument, and all items in the square brackets are associative arguments.

When executing the above WP-CLI command, you only need one positional argument to point WP-CLI to the plugin you’re trying to install. The associative arguments are optional. So, you can skip them, use one of them, or use multiple to suit your use case.

例如,您可以安装WordPress Jetpackplugin using the following command that adds a positional argument to the primary command:


 

$ WP插件安装喷气背包


Here's a live view of what this might look like on your site:

WP-CLI安装插件

And, if you’d like to install and activate the plugin at the same time, you can add an associative argument as shown below:


 

$ WP插件安装喷气背包--activate


This is what it would look like in action:

WP-CLI jetpack plugin

This eliminates the need to enter multiple commands via the command line.

如何使用WP-CLI管理WordPress网站

WP-CLI提供数百个通用命令,并可以支持无限数量的自定义命令。因此,使用WP-CLI来管理WordPress网站的可能性实际上是无限的。

To help you start, we’ve put together a list of examples that demonstrate how to use the WP-CLI for routine tasks.

1.下载并安装WordPress。

You can use the WP-CLI to install WordPress by downloading or using a zip file.

To download WordPress from theWordPress.orgservers, use the following command:


 

$ wp core download


Live Example:

download wordpress wp-cli - wp cli install wordpress.

此命令下载核心应用程序文件,验证MD5 checksum, and then extracts the files to the current directory. You can also specify a path by adding - 路径= followed by the direct path to the download destination at the end of the command.

船尾er downloading WordPress, you’ll need to configure the wp-config usingwp core config, update file permissions for the wp-config.php file, and then install WordPress using the below command:


 

$ wp core install -url = example.com -title = example -admin_user = supervisor -admin_password = strongpassword -admin_email = info@example.com


确保将关联参数中“ =”之后的所有值替换为所需值。

2.更新和回滚WordPress版本。

您也可以使用WP-CLI到update WordPress或降级到较旧版本。

更新使用WP-CLI WordPress,运行following command:


 

$ WP核心更新


This command updates WordPress to the latest release available on WordPress.org. You can also add associative arguments to:

  • 定义通往ZIP文件以安装WordPress的路径。
  • 指定WordPress的版本。
  • force WordPress to roll back to an older version.

Here’s how you could rollback WordPress from version 5.8.2 to 5.8.1:


 

$ WP核心更新--version=5.8.1 --force


As a general best practice, you should always use the latest version of WordPress. However, if you encounter issues such as plugin or theme incompatibilities, you can roll back to an older version until the developer releases a compatible update to their plugin.

3.执行WordPress健康检查。

您也可以使用WP-CLI到troubleshoot your WordPress installation using a WP-CLI package calledWP Doctor。This software runs a series of checks for common WordPress issues such as failed updates, caching issues, and cron misconfigurations.

You can install the WP Doctor package using the following command:


 

$ wp package install git@github.com:wp-cli/doctor-command.git


Live Example:

WP-CLI健康检查

船尾er this, you can run a complete check using the following command:


 

$ wp doctor check --all


Live Example:

WP-CLI健康检查example

船尾er thewp doctor命令执行,它输出执行的检查的名称和状态,以及一条消息,以帮助您诊断任何错误。当您的网站从蓝色中掉下来时,这可以节省您数小时的故障排除。

4. Change your WordPress URL.

通常,如果要更改WordPress URL,则可以使用以下方式更新它:

  • WordPress admin panel
  • 数据库
  • wp-config.php file

但是,每一个都有其自身的复杂性。WP-CLI提供了一种更清洁的方法来更新您的网站URL。

For example, if you want to update your WordPress URLs from HTTP to HTTPS, you can do so as follows:


 

$ WP选项更新主页'https://www.example.com'

$ WP选项更新SiteUrl'https://www.example.com'


Live Example:

WP-CLI change URL

5.列出所有WordPress插件。

WP-CLI使您能够输出网站上安装的所有插件的详细列表,并提供其他信息,例如其激活状态,更新可用性和版本,如下所示:


 

$ wp plugin list


Live Example:

WP-CLI all plugins

You can even add options or arguments to this command to modify its behavior. For example, to list the installed drop-ins on your website, you can use the following command:


 

$ wp plugin list --status=dropin


6. Install WordPress plugins.

您也可以使用WP-CLI到install a plugin on your WordPress website from the command line as follows:


 

$ wp plugin install


For example, here’s how you can install and activate theHubSpot WordPress插件使用WP-CLI插件命令在您的网站上:


 

$ wp plugin install leadin --activate


Live Example:

install and activate plugin wp-cli

You can even use the WP-CLI toupdate plugins使用wp plugin updatecommand.

7. Uninstall WordPress plugins.

Although rare, sometimes, a plugin update might break your website. In such cases, the WP-CLI offers you an alternative method to uninstall plugins without using the WordPress admin.

To uninstall plugins on your WordPress website using the WP-CLI, you can use thewp plugin uninstallcommand. For example:


 

$ wp plugin uninstall hello


Live Example:

uninstall plugin WP CLI

此命令卸载并删除Hello插件。如果您希望在不删除插件的情况下卸载插件,则可以使用以下命令:


 

$ wp plugin uninstall hello --skip-delete


8. Install WordPress themes.

InstallingWordPress themes使用WP-CLI is quicker than doing it from the admin. You can do it using thewp theme installcommand as follows:


 

$ wp theme install twentytwenty


Live Example:

安装主题WP-CLI

You can add the - 激活option to the command to activate the theme after installing it and specify a theme version using the--version=选项。

9. Uninstall WordPress themes.

A poorly-developed theme can break your website. And, when you can’t access the WordPress admin, the only way to disable the theme is to either rename or delete the theme directory in the WordPress installation folder.

The WP-CLI offers an easier way to卸载主题。You can use thewp theme deletecommand to remove a theme from your website as follows:


 

$ WP主题删除二十一


Live Example:

WP CLI maintenance mode

Thewp theme deletecommand doesn’t delete an active theme by default. If you want to remove an active theme from your website, you’ll need to add the--forceoption to the end of the command.

10. Manage WordPress maintenance mode.

WordPress maintenance mode暂时禁用访问前端的哟ur website. This is important because it prevents website visitors from interacting with your website during theme or plugin updates.

但是,有时,即使更新完成后,WordPress也可能仍处于维护模式。不幸的是,WordPress不允许您从管理员中禁用维护模式。

如果您的WordPress网站陷入维护模式,则需要通过SSHand delete the .maintenance file in the root folder.

Alternatively, you can also use the WP-CLI to manage the maintenance mode of your website.

You can enable, disable, and check its status using the following commands:


 

$ wp maintenance-mode activate

$ wp maintenance-mode status

$ WP维护模式停用


Live Example:

WP CLI maintenance mode example

11. Manage user roles.

WordPress可让您通过管理员分配用户角色。而且,如果您需要自定义角色,则可以使用插件或编辑functions.php文件来完成。

The WP-CLI offers an easier way to create, assign, and remove user roles. It also helps you perform advanced user administration tasks such as listing and editing user capabilities and even destroying user sessions.

例如,您可以如下创建一个新的用户角色:


 

$ WP角色创建设计师设计师



You can then assign capabilities to the new role as follows:

 

$ wp cap add designer spectate


Finally, you can assign this role to an existing user using the following command:


 

$ wp user set-role 2 designer


Live Example:

user roles WP CLI

常见的WP-CLI错误以及如何修复它们

When working with the WP-CLI, you can run into errors occasionally. Here are four common WP-CLI errors and their solutions.

1. Error: Can’t Connect to the Database

通常,当WP-CLI无法连接到网站数据库时,显示“错误:无法连接到数据库”消息。这个连接问题可能来自几个不同的因素,但通常的嫌疑人是:

  1. 多站点WordPress安装
  2. MAMP is not using the MAMP PHP Binary
  3. Incorrect database credentials in the wp-config.php file

The solution to this problem varies based on the cause. If you’re running a multisite installation, you’ll need to use the WP网站command.

如果您使用的是MAMP和MAMP不使用MAMP PHP二进制文件,则可以配置PHP CLI以使用MAMP的PHP。

And, if the database credentials in wp-config.php are incorrect, update the values and rerun the command.

2. PHP Fatal Error: Allowed Memory Size of 999999 Bytes Exhausted (Tried To Allocate 99 Bytes)

通常,当服务器用完内存时,您通常会看到此错误消息。

WP-CLI使用流行的依赖管理器Composer来管理WP-CLI软件包。而且,不幸的是,作曲家可以在诸如软件包安装之类的任务中耗尽服务器内存。

But the good news is that this one’s an easy fix. You can solve this problem by editing thePHP配置文件, aka php.ini. Use your preferred text editor and update the value for “memory_limit” to at least 512M or more, as demonstrated below.

PHP配置文件

Finally, restart PHP to update the configuration.

3. PHP Fatal Error: Call to Undefined Function

WP-CLI checks the wp-config.php file for important information like the database access credentials before it loads. And because WP-CLI doesn’t want to load WordPress, it uses regular expressions to strip the require_once(ABSPATH . ‘wp-settings.php’); statement.

如果您修改了wp-config.php文件以调用WordPress函数,则会看到“呼叫到未定义的函数”错误。发生这种情况是因为PHP无法调用WordPress函数。从配置文件中删除所有WordPress函数应将其修复。

You should avoid using the wp-config.php file to call functions. It’s better to move those modifications to amust-use pluginso WP-CLI can parse the config file without errors.

4. WP-CLI Doesn’t Load With the WordPress Install

Although rare, sometimes you might find that WP-CLI doesn’t load with your WordPress install. This is often caused by a theme or plugin that conflicts with the load process.

If you find that WP-CLI doesn’t load despite your best efforts, you can load it without loading themes and plugins using the following command:


 

$ wp -skip-plugins -skip-themes


If WP-CLI loads after this, you can find the conflicting theme or plugin by running the command using only one of the two arguments. For example:


 

$ wp --skip-themes


If WP-CLI loads after this, you’ll know the active theme is the problem. And if it doesn’t, you can try to load WP-CLI while skipping one plugin at a time as follows:


 

$ wp --skip-plugins=hello


确定矛盾的插件后,您可以停用或卸载它。

Managing Your WordPress Websites With WP-CLI

WP-CLI is a powerhouse of infinite possibilities. Whether you’re a first-time user or a seasoned marketer, WP-CLI ensures you can manage WordPress websites effortlessly. Use the tips above to get started, and continue to explore all of the commands available with this handy WordPress function.

Use HubSpot tools on your WordPress website and connect the two platforms  without dealing with code. Click here to learn more.

Use HubSpot tools on your WordPress website and connect the two platforms  without dealing with code. Click here to learn more.

最初于2022年1月20日上午7:00 am发行,更新于2022年1月20日

Topics:

WordPress Website