-wp -md_WP-CLI自动化的5种节省时间的用途

-wp -md

WP-CLI Automation

This article is part of a series created in partnership with SiteGround. Thank you for supporting the partners who make SitePoint possible.

本文是与SiteGround合作创建的系列文章的一部分。 感谢您支持使SitePoint成为可能的合作伙伴。

什么是WP-CLI? (What is WP-CLI?)

The WP-CLI is a tool that allows you to perform actions on a WordPress installation straight from the command line. WP-CLI automation is the automating of repetitive manual tasks by using WP-CLI driven scripts. This may seem unnecessary, awkward, or too difficult a task to bother with when you’re deploying or managing a single WordPress installation, but when you’re managing many, or constantly creating similar new sites for testing, it becomes an extremely valuable tool to have in your developer toolkit.

WP-CLI是一个工具,可让您直接从命令行对WordPress安装执行操作。 WP-CLI自动化是通过使用WP-CLI驱动的脚本来自动执行重复的手动任务。 在部署或管理单个WordPress安装时,这看起来似乎是不必要,尴尬或太麻烦的任务,但是当您管理多个WordPress或不断创建类似的新站点进行测试时,它成为非常有价值的工具包含在您的开发人员工具包中。

关于WP-CLI (About WP-CLI)

With WP-CLI, you can essentially perform any action that you could have via the admin panel, but from the command line instead. You can install or update core WordPress files, plugins, or themes. You can activate and deactivate plugins or regenerate image thumbnails. You can also perform database actions, such as export and import of the database, or find and replace the database for information, such as a changed URL during a migration.

使用WP-CLI,您基本上可以通过管理面板执行任何操作,但是可以从命令行执行。 您可以安装或更新WordPress核心文件,插件或主题。 您可以激活和停用插件或重新生成图像缩略图。 您还可以执行数据库操作,例如数据库的导出和导入,或者查找和替换数据库以获取信息,例如在迁移过程中更改URL。

Some plugins have WP-CLI support as well — including many of the more popular ones. This means you can set up automated scripting to install and set up WordPress, install those plugins, and then to set up the plugins as well, using their own customized WP-CLI commands!

一些插件也支持WP-CLI-包括许多更流行的插件。 这意味着您可以设置自动化脚本来安装和设置WordPress,安装这些插件,然后使用它们自己的自定义WP-CLI命令来设置插件!

WP-CLI自动化 (WP-CLI Automation)

WP-CLI automation goes beyond simple command line usage when setting up or managing multiple WordPress installations. The ability to update or back up multiple sites at once, or create complicated boilerplate installations repeatedly with single commands are incredibly useful and can save a significant amount of time for maintainers of those sites.

设置或管理多个WordPress安装时,WP-CLI自动化不仅限于简单的命令行用法。 一次更新或备份多个站点,或者使用单个命令重复创建复杂的样板安装的能力非常有用,并且可以为那些站点的维护人员节省大量时间。

If you don’t already have the WP-CLI installed, take a look at the installation documentation and get the WP-CLI up and running.

如果尚未安装WP-CLI,请查看安装文档并启动并运行WP-CLI。

WP-CLI自动化的五个用例 (Five Use Cases for WP-CLI Automation)

安装WordPress (Installing WordPress)

Once WordPress is installed, this example script could download, configure, and install WordPress core, remove starting plugins, add and activate a specified theme (saved in example-theme.zip), and install and activate a list of plugins you’d prefer to use with new installations.

安装WordPress后,此示例脚本可以下载,配置和安装WordPress核心,删除启动的插件,添加并激活指定的主题(保存在example-theme.zip中),以及安装并激活您喜欢的插件列表。与新安装一起使用。

Example:

例:

#!/usr/bin/env bash

#plugins to install and activate (slugs)
WPPLUGINS=( test-plugin1 test-plugin2 test-plugin3 )

echo "Starting WordPress Installation Script"

# Site Name Input
echo "Site Name: "
read -e sitename

# Site URL Input
echo "Site URL: "
read -e siteurl

# Download WP and configure it
wp core download
wp core config --dbname=$dbname --dbuser=root --dbpass=root
wp db create
wp core install --url=$siteurl --title="$sitename" --admin_user="admin" --admin_password="examplePassword123" --admin_email="test@example.com"

# Remove default plugins, install plugins, install Base Theme
wp plugin delete --all
wp theme install example-theme.zip --activate
wp plugin install ${WPPLUGINS[@]} --activate

echo "WordPress installation complete!"

However, you could automate this process even further, by asking the user for relative path information, so that you don’t have to be in the installation directory to run it, by asking for database name and password, and more. You can also do (as you’ll see later in this article) a setup for a hosting environment that handles multiple WordPress installations on one server, and set up and install more than one site at once. Customize the script in the way that you need, so that it can be maximally effective for your own projects, and so that you won’t have to constantly rewrite it — make it efficient!

但是,您可以通过询问用户相对路径信息来进一步实现此过程的自动化,这样您就不必在安装目录中运行它,也不需要询问数据库名称和密码等等。 您还可以为托管环境做一个设置(如您将在本文后面看到的那样),该环境可以在一台服务器上处理多个WordPress安装,并且可以一次设置并安装多个站点。 以您需要的方式自定义脚本,以使其对您自己的项目发挥最大作用,并且您不必不断重写它-提高效率!

备份WordPress (Backing Up WordPress)

Backing up your WordPress installation is a must, but there are a variety of ways to do it. You can backup easily with a number of WordPress backup plugins, but you can also do so straight from the command line.

备份WordPress安装是必须的,但是有多种方法可以做到。 您可以使用许多WordPress备份插件轻松进行备份 ,但也可以直接从命令行进行备份

First, you’ll want to run (whether at the command line, or via a script) wp db export example.com_20170501T1420 from the website’s directory, with the last parameter being the filename you prefer. Of course, if automating that process entirely, it would be handy to add in a timestamp to file names.

首先,您将要运行(无论是在命令行中还是通过脚本)从网站目录中运行wp db export example.com_20170501T1420 ,最后一个参数是您喜欢的文件名。 当然,如果完全自动化该过程,则在文件名中添加时间戳会很方便。

Once that is done, your website’s root directory will contain a .sql file which is a backup of the site’s database at the time it was exported. You can then run a simple tar -vczf example.com_20170501T1420.gz . (using the same file name for this backup archive), which will compress both the website’s files, and the .sql file along with it. Now, via the command line, a script, or an SFTP client, you can copy that archive file to another computer, drive, or cloud storage, a backup of both files and database, within moments!

完成此操作后,您网站的根目录将包含一个.sql文件,该文件是该网站在导出时的备份。 然后,您可以运行一个简单的tar -vczf example.com_20170501T1420.gz . (为此备份归档文件使用相同的文件名),这将同时压缩网站的文件以及.sql文件。 现在,通过命令行,脚本或SFTP客户端,您可以立即将存档文件复制到另一台计算机,驱动器或云存储中,这是文件和数据库的备份!

WordPress核心更新 (WordPress Core Updates)

To update the WordPress core files for the site in your current directory, run the wp core update command. This command really shines when you set up a script to loop through a list of the installations on the current server, updating each in turn, all by entering a single command.

要更新当前目录中站点的WordPress核心文件,请运行wp core update命令。 当您设置脚本以遍历当前服务器上的安装列表并依次更新每个安装(通过输入单个命令)时,此命令确实很有用。

Example:

例:

#!/usr/bin/env bash

# Assumes site directories are under /var/www/siteurl

WPSITES=( example.com example2.com example3.com )

WPPATH=/var/www/

echo "Starting WordPress Core Updates"

for i in "${WPSITES[@]}"
do
  : 
  wp core update --path:$WPPATH$i
  echo "Updates for $i Completed!"
done

echo "WordPress Core Updates Complete!"

WordPress插件和主题更新 (WordPress Plugin and Theme Updates)

Similarly to the core updates, loop through a list of your sites, running wp plugin update -all to update all plugins installed on each site, or wp theme update --all to do the same for themes.

与核心更新类似,循环浏览您的站点列表,运行wp plugin update -all以更新每个站点上安装的所有插件,或者运行wp theme update --all对主题执行相同的操作。

Example:

例:

#!/usr/bin/env bash

# Assumes site directories are under /var/www/siteurl

WPSITES=( example.com example2.com example3.com )

WPPATH=/var/www/

echo "Starting WordPress Plugin and Theme Updates"

for i in "${WPSITES[@]}"
do
  : 
  wp plugin update --all --path:$WPPATH$i
  wp theme update --all --path:$WPPATH$i
  echo "Updates for $i Completed!"
done

echo "WordPress Plugin and Theme Update Complete!"

If you wish to do core WordPress updates as well as plugins and themes, you could also combine those into one update script.

如果您希望进行WordPress核心更新以及插件和主题,还可以将它们合并为一个更新脚本。

WordPress迁移 (WordPress Migrations)

As a part of your migration flow, when migrating a site between servers, to another domain, or between development and production or staging environments, you can handle all of your database concerns with WP-CLI as well.

作为迁移流程的一部分,当在服务器之间,另一个域或开发与生产或暂存环境之间迁移站点时,您也可以使用WP-CLI处理所有数据库问题。

Export the database from your old hosting server (run from the website root directory) with:

使用以下命令从旧的托管服务器(从网站根目录运行)中导出数据库:

wp db export example.com_20170501T1420

Import it to your new hosting server (run from the website root directory) with:

使用以下命令将其导入到新的托管服务器(从网站根目录运行):

wp db import example.com_20170501T1420

Then replace old information (like a URL) with new information (run from the website root directory) with:

然后将新信息(从网站根目录运行)替换为旧信息(如URL),方法是:

wp search-replace oldurl.com newurl.com

The search-replace command replaces any instance of oldurl.com with newurl.com).

search-replace命令替换的任何实例oldurl.comnewurl.com )。

This process could also be automated, by extending the same scripts you might use for a backup. You could easily have an export script, then an import script that has added inputs for search and replace fields, and perhaps even extend it with options for new database credentials, if they’ve changed.

通过扩展与备份相同的脚本,该过程也可以自动化。 您可以轻松地拥有一个导出脚本,然后是一个导入脚本,该脚本添加了用于搜索和替换字段的输入,甚至可以更改新数据库凭据的选项(如果它们已更改)。

结论 (Conclusions)

The number of tasks that can be automated with WP-CLI is simply amazing. You can customize an installation script to download WordPress core, create your configuration and your database, install WordPress, strip it of any bloat, add default plugins and themes and activate them, and more. You can also use it to run backups, updates, migrations, and more.

WP-CLI可以自动执行的任务数量简直令人惊讶。 您可以自定义安装脚本,以下载WordPress核心,创建配置和数据库,安装WordPress,将其浮出水面,添加默认插件和主题并激活它们,等等。 您还可以使用它来运行备份,更新,迁移等。

Choosing a good host is important when you want to use WP-CLI. Many hosts don’t support the usage of WP-CLI, so finding one that does is of paramount importance if you intend to utilize WP-CLI automation. SiteGround is one of the hosts that actively supports and invests in the maintenance of the WP-CLI project. It’s a great choice for hosting your WordPress website, especially when you need to use WP-CLI — it has WP-CLI enabled on all WordPress hosting plans. SiteGround also has a useful tutorial on using WP-CLI on their servers.

当您想使用WP-CLI时,选择一个好的主机很重要。 许多主机不支持WP-CLI的使用,因此,如果您打算使用WP-CLI自动化,找到一个兼容的主机至关重要。 SiteGround是积极支持和投资WP-CLI项目维护的主机之一。 这是托管WordPress网站的绝佳选择,尤其是当您需要使用WP-CLI时-它在所有WordPress托管计划中都启用了WP-CLI。 SiteGround还提供了有关在服务器上使用WP-CLI的有用教程

Check them out, and get to work automating your installation and maintenance of WordPress with WP-CLI!

检查它们,并开始使用WP-CLI自动执行WordPress的安装和维护!

翻译自: https://www.sitepoint.com/5-time-saving-uses-wp-cli-automation/

-wp -md

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值