使用WP-CLI管理WordPress入门指南

tech2022-08-08  117

This article provides an introductory guide to WP-CLI, a command-line tool that was created to make developers’ lives easier, allowing them to manage a WordPress site through the command line rather than through the usual admin interface.

本文提供了WP-CLI的入门指南,WP-CLI是一种命令行工具,旨在使开发人员的生活更轻松,使他们可以通过命令行而不是通常的管理界面来管理WordPress网站。

WP-CLI was created by Daniel Bachhuber over a decade ago. Since then, it’s become an indispensable tool in every advanced WordPress developer’s arsenal — “deployed and relied upon by almost every major user of WordPress”, according to Matt Mullenweg. Since 2016, WP-CLI has been an official WordPress CLI tool.

WP-CLI由Daniel Bachhuber于十年前创立。 自那时以来,它已经成为每一个先进的WordPress开发者的武器库中不可或缺的工具- “部署和由WordPress的几乎每一个主要的用户依靠”,根据马特·查尔斯·穆伦维格 。 自2016年以来,WP-CLI一直是官方的WordPress CLI工具 。

WP-CLI is used for installing and setting up a WordPress website, changing its options, administering users, and a host of other things. It can be leveraged to significantly speed up developers’ workflows.

WP-CLI用于安装和设置WordPress网站,更改其选项,管理用户以及许多其他事情。 可以利用它来显着加快开发人员的工作流程。

WP-CLI comes as a phar file — short for PHP Archive. It’s a standard for packaging multiple PHP files and other resources as a single application — for simpler distribution and installation.

WP-CLI作为phar文件提供phar Archive的缩写。 它是将多个PHP文件和其他资源打包为一个应用程序的标准-以便更轻松地分发和安装。

安装 (Installation)

WP-CLI presumes, obviously, that we have access to the system shell. This will be pretty straightforward on Linux and macOS systems — particularly on servers — as WordPress is served almost universally from Linux machines. If we have dedicated server hosting, or cloud hosting like AWS, Alibaba Cloud, etc., or if we’re using a VPS from Digital Ocean, Vultr, Linode and the like, SSH comes as a default access option, but these days many shared hosts offer SSH access options. (Some might even come with WP-CLI preinstalled.)

显然,WP-CLI假定我们可以访问系统外壳。 在Linux和macOS系统上,尤其是在服务器上,这将非常简单,因为WordPress几乎都是从Linux计算机上提供的。 如果我们拥有专用的服务器托管,或诸如AWS,阿里云等的云托管,或者我们使用的是Digital Ocean,Vultr,Linode等的VPS,则SSH是默认的访问选项,但如今很多时候共享主机提供SSH访问选项。 (有些甚至可能预装了WP-CLI。)

For Windows users, WP-CLI can be installed via Composer, but we recommend readers get themselves acquainted with Windows Subsystem for Linux, because it makes it possible to have a native Linux environment available, along with Bash, package manager like APT, etc. WordPress is a PHP app, and PHP’s native environment is Linux.

对于Windows用户,可以通过Composer安装WP-CLI,但是我们建议读者熟悉Linux的Windows子系统 ,因为它可以使本机Linux环境以及Bash,包管理器(如APT)等成为可能。 WordPress是一个PHP应用程序,而PHP的本机环境是Linux。

Further code samples presume we’re using Linux or a Unix-type system.

进一步的代码示例假定我们正在使用Linux或Unix类型的系统。

To fetch the WP-CLI phar archive, we usecurl or wget:

要获取WP-CLI phar存档,我们使用usecurl或wget :

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

This downloads our archive to the current directory. Then we make it executable:

这会将我们的存档下载到当前目录。 然后我们使其可执行:

chmod +x wp-cli.phar

We them move it so it’s available as a wp command:

我们将它们移动,因此可以作为wp命令使用:

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

Now we have a wp command available:

现在我们有一个wp命令可用:

Now, upon typing the wp command, it displays all the options for us, and possible parameters. One caveat: if we’re running as the root user, we need to add --allow-root to our commands:

现在,在键入wp命令后,它将显示我们的所有选项以及可能的参数。 一个警告:如果我们以root用户身份运行,则需要在命令中添加--allow-root :

Now that we have it set up, we can explore the commands, and possible usage scenarios.

现在我们已经设置好了,我们可以探索命令和可能的使用场景。

WP-CLI命令 (WP-CLI Commands)

WP-CLI aims to offer a fast alternative to the WordPress web admin interface. There are chunks of code or functionality that offer simple, precise interfaces for performing complex tasks. Beside the bundled commands, WP-CLI defines an API for integrating third-party commands — WP_CLI::add_command(). These can be distributed either as standalone packages, or as part of WordPress plugins or themes.

WP-CLI旨在提供一种替代WordPress Web管理员界面的快速替代方法。 有许多代码或功能块提供了简单,精确的接口来执行复杂的任务 。 除了捆绑的命令外,WP-CLI还定义了一个用于集成第三方命令的API- WP_CLI :: add_command() 。 这些可以作为独立包分发,也可以作为WordPress插件或主题的一部分分发。

In this guide, we’ll review bundled commands — those that come with the default WP-CLI installation — and some more notable third-party commands.

在本指南中,我们将检查捆绑的命令(默认WP-CLI安装附带的命令)以及一些更著名的第三方命令。

Commands can come as basic, one-argument commands like wp somecommand, or as subcommands under the base command namespace, such as wp somecommand subcommand.

命令可以是基本的一元命令,例如wp somecommand ,也可以是基本命令名称空间下的子命令,例如wp somecommand subcommand 。

wp核心 (wp core)

The wp core subcommand is a command/namespace that consists of sucommands that deal with WordPress core — so we can download, install, update, convert to multisite and get information about our WordPress core version:

wp core 子命令是一个命令/命名空间,由处理WordPress核心的sucommand组成-因此我们可以下载,安装,更新,转换为多站点并获取有关WordPress核心版本的信息:

wp core download will download the latest version of WordPress into the current directory

wp core download会将WordPress的最新版本下载到当前目录中

wp core install runs the standard WordPress installation process, with options like --url=somewebsite.com, --title=SomeWebsite, --admin_user=someusername, --admin_password=somepassword and --admin_email=some@email.com

wp core install运行标准的WordPress安装过程,并带有--url=somewebsite.com ,-- --title=SomeWebsite ,-- --admin_user=someusername ,-- --admin_password=somepassword和--admin_email=some@email.com

wp core multisite-install installs a new multisite WordPress installation, and wp core multisite-convert converts a regular installation into multisite.

wp core multisite-install将安装新的多站点WordPress安装,而wp core multisite-convert将常规安装转换为多站点。

wp core update will update WordPress to a newer version, and wp core update-db will update the database.

wp core update将把WordPress更新到一个新版本,而wp core update-db将更新数据库。

More details on wp core can be found in the documentation.

有关wp core的更多详细信息,请参见文档 。

WP-CLI really shines when we combine its commands in Bash scripts, so we can combine, for example, wp core download and wp core install into a single Bash command and streamline the installation.

当我们在Bash脚本中组合其命令时,WP-CLI确实令人眼前一亮,因此我们可以将wp core download和wp core install到单个Bash命令中,从而简化安装。

Worth noting here is that before we run the installation, we need to create a wp-config.php file, with database credentials and other details needed for the installation.

值得一提的是,在运行安装之前,我们需要创建一个wp-config.php文件,其中包含数据库凭据和安装所需的其他详细信息。

WP-CLI provides a wp config create command for this.

WP-CLI为此提供了wp config create命令。

WP配置 (wp config)

wp config is a namespace for commands that deal with WordPress configuration.

wp config是处理WordPress配置的命令的命名空间。

wp config list lists all the configuration variables:

wp config list列出了所有配置变量:

wp config create — as we said — creates configuration file with variables we provide, like wp config create --dbname=somedb --dbuser=someuser --dbpass=somepass, and other variables, as outlined in the docs

wp config create -正如我们所说-创建配置文件与我们提供的变量,如wp config create --dbname=somedb --dbuser=someuser --dbpass=somepass和其他变量,如概述中的文档

wp config get (for example, wp config get table_prefix) fetches specific config variables from wp-config.php

wp config get (例如, wp config get table_prefix )从wp-config.php获取特定的配置变量

wp config set, similarly, sets config variables

wp config set ,类似地,设置配置变量

More of the wp config details can be found here.

可以在此处找到更多wp config详细信息。

wp cap is interesting for administering user roles and capabilities. We can add and remove capabilities from particular roles.

wp cap对于管理用户角色和功能很有趣。 我们可以添加和删除特定角色的功能。

wp cron is a command namespace for testing, running and deleting WP-Cron events. wp cron event list, for example, would give us the output looking something like this:

wp cron是用于测试,运行和删除WP-Cron事件的命令名称空间。 例如, wp cron event list将为我们提供如下输出:

Then we could delete events with something like wp cron event delete wsal_cleanup — or reschedule them, etc.

然后,我们可以使用wp cron event delete wsal_cleanup类的东西来删除事件,或者重新安排它们的时间,等等。

Sometimes, in the course of updating content, developing, making changes, we’ll find that refreshing a WordPress page will not show the changes we made. Many times this has resulted in a frantic search, trying to find what we did wrong.

有时,在更新内容,开发,进行更改的过程中,我们会发现刷新WordPress页面不会显示我们所做的更改。 很多时候,这导致了疯狂的搜索,试图找到我们做错了什么。

Often it’s a cache issue. WordPress Object Cache, by default, isn’t persistent, so the need to clean the object cache will be exacerbated with the use of plugins that persist object cache across requests (and this is usually the case).

通常这是一个缓存问题。 默认情况下,WordPress对象缓存不是持久性的,因此使用可在请求中持久保存对象缓存的插件会加剧清理对象缓存的需求(通常是这种情况)。

wp cache is a namespace that contains commands for handling the WP Object Cache.

wp缓存是一个命名空间,其中包含用于处理WP对象缓存的命令。

wp cache flush is a command that flushes the whole cache. It’s a no-brainer — a simple, oft-used command that doesn’t require any other parameters, and purges everything from the cache.

wp cache flush是刷新整个缓存的命令。 这很容易-一个简单,经常使用的命令,不需要任何其他参数,并且可以从缓存中清除所有内容。

wp cache contains other commands, as well, that can be used for very atomic management of cache items.

wp cache包含其他命令,这些命令可用于非常原子化的缓存项管理。

wp cache add, wp cache delete, wp cache get, wp cache set, wp cache replace and other commands make it possible to list, inspect, add, change or delete specific values from the object cache.

wp cache add , wp cache delete , wp cache get , wp cache set , wp cache replace和其他命令使从对象缓存中列出,检查,添加,更改或删除特定值成为可能。

WordPress transients are another element of WP caching strategy, which is persistent by default, and can play a part in WordPress’s overall performance. It’s not unheard of that many plugins liberally use WordPress transients, which can get cluttered and slow down the website.

WordPress瞬态是WP缓存策略的另一个元素,默认情况下是持久的,并且可以在WordPress的整体性能中发挥作用。 并非没有听说过许多插件会大量使用WordPress瞬态,这些瞬态会变得混乱并降低网站速度。

The wp transient namespace contains commands to delete, get or set transients.

wp瞬态名称空间包含用于删除,获取或设置瞬态的命令。

Another element in the WordPress caching system that sometimes requires flushing, and has probably caused hours and hours of confusion for the beginners, are WordPress permalinks.

WordPress 永久链接是WordPress缓存系统中的另一个元素,有时它需要冲洗,并且可能对初学者造成数小时的混乱。

wp rewrite — wp rewrite flush in particular — makes it possible to flush rewrite rules (permalinks). We can also list rewrite rules.

wp wp rewrite flush特别是wp rewrite flush使刷新规则(永久链接)成为可能。 我们还可以列出重写规则。

wp db contains commands for managing a WordPress database. Insights, repair, optimization, search, various queries. We can also export or import the database.

wp db包含用于管理WordPress数据库的命令。 见解,维修,优化,搜索,各种查询。 我们还可以导出或导入数据库。

wp eval and wp eval-file can be used to execute some code in the context of our WordPress installation.

在我们的WordPress安装环境中,可以使用wp eval和wp eval文件执行一些代码。

wp export and wp import export and import content in WXR format.

wp导出和wp导入以WXR格式导出和导入内容。

wp option contains commands for managing, getting and setting WordPress options.

wp选项包含用于管理,获取和设置WordPress选项的命令。

wp scaffold contains commands that create boilerplate or starting code for plugins, themes, child themes, Gutenberg blocks, post types, taxonomies — thus shortening the path to get them running.

wp支架包含用于为插件,主题,子主题,Gutenberg块,帖子类型,分类法创建样板或启动代码的命令,从而缩短了使它们运行的​​路径。

wp search-replace does search-replace on a database with strings we provide it as arguments. This comes very handy when we migrate the database from one website to another, and need to change URLs. For example, when we create a staging website, or move a database from staging to production website.

wp search-replace使用我们提供作为参数的字符串在数据库上进行搜索-替换。 当我们将数据库从一个网站迁移到另一个网站并需要更改URL时,这非常方便。 例如,当我们创建暂存网站或将数据库从暂存网站移至生产网站时。

WordPress serializes content strings in the database, so doing a raw search–replace on a database in some editor wouldn’t work; it would, in fact, break the website.

WordPress会序列化数据库中的内容字符串,因此无法在某些编辑器中对数据库进行原始搜索替换; 实际上,它会破坏网站。

wp shell is particularly interesting, as it allows us to enter a WordPress repl — a live shell environment of our WordPress installation. There we have full access to everything that some active plugin may have available. We can write code, load code from files, execute functions, observe or inspect output from functions. It makes it very easy to test out new code without browser refresh cycles.

wp外壳特别有趣,因为它允许我们输入WordPress副本-WordPress安装的实时外壳环境。 在这里,我们可以完全访问某些活动插件可能提供的所有内容。 我们可以编写代码,从文件中加载代码,执行函数,观察或检查函数的输出。 它使测试新代码变得非常容易,而无需浏览器刷新周期。

wp user is for managing, updating, deleting and changing roles of users.

wp用户用于管理,更新,删除和更改用户角色。

These are some of the default, built-in commands. Detailed documentation of all the commands is available in the WordPress developer docs.

这些是一些默认的内置命令。 WordPress开发人员文档中提供了所有命令的详细文档。

wp plugin makes it possible to list, install, activate, deactivate and delete plugins, and to write scripts that automate installation of multiple plugins in bulk. wp plugin list might give us output looking something like this:

wp插件使列出,安装,激活,停用和删除插件成为可能,并编写了自动批量安装多个插件的脚本。 wp plugin list可能使我们的输出看起来像这样:

wp theme does the same, only for themes.

wp主题仅针对主题执行相同的操作。

wp package is a command namespace for managing WP-CLI packages. With wp package install somepackagename we can install third-party packages that are added to the WP-CLI Package index. Some noteworthy packages/commands are: wp usergen cli, which creates random users for testing; db checkpoint, which creates db snapshots; WP-CLI buddypress, which contains a number of BuddyPress-related commands; WP-CLI size, which shows database and table sizes; wp hook, which shows callback functions registered for a particular filter or action hook; query debug, which debugs the performance of queries; and faker, which helps us create filler posts for development purposes.

wp软件包是用于管理WP-CLI软件包的命令名称空间。 使用wp package install somepackagename我们可以安装添加到WP-CLI Package index中的第三方软件包。 一些值得注意的包/命令是: wp usergen cli ,它创建随机用户进行测试; db checkpoint ,它创建数据库快照; WP-CLI buddypress ,其中包含许多与BuddyPress相关的命令; WP-CLI size ,它显示数据库和表的大小; wp hook ,显示为特定过滤器或动作钩子注册的回调函数; query debug ,它调试查询的性能; 和faker ,这有助于我们创建用于开发目的的加油站。

There are many other packages/commands maintained by the community. The full list can be foud here.

社区还维护着许多其他软件包/命令。 完整列表可以在这里找到 。

结论 (Conclusion)

In this guide, we introduced WP-CLI and covered its main commands. We also introduced some of its third-party packages, but this is in no way complete reference. With WP-CLI, the usage possibilities and scenarios are virtually endless.

在本指南中,我们介绍了WP-CLI,并介绍了其主要命令。 我们还介绍了其一些第三方软件包,但这绝不是完整的参考。 使用WP-CLI,使用可能性和方案几乎是无限的。

翻译自: https://www.sitepoint.com/wp-cli-introduction/

最新回复(0)