WP-CLI 是个控制台终端命令行扩展工具,方便大家在命令行里直接维护 WordPress 站点的更新、升降级、数据库、插件、主题、翻译等等可以说几乎囊括了所有 WordPress 的操作都可以在命令行里实现,可以说做运维的话 WP-CLI 是必不可少的要掌握的,这可以让你的 WordPress 服务器运维工作效率提高很多,明月在【使用 WP CLI 管理 WordPress 也是蛮有意思的!】一文里已经做过简单的介绍了,但是涉及 WordPress 管理的命令仅仅是示例而已,并不是很全面,今天明月就给大家收集整理了日常要用的指令,就当是个参考手册吧。

A-Complete-Beginners-Guide-to-WP-CLI-800x500.jpg

安装

先使用 ssh 登录到服务器,然后进入到某个目录以后,使用 wget 或者 curl 命令去下载 wp-cli

curl -L https://raw.github.com/wp-cli/builds/gh-pages/phar/wp-cli.phar > wp-cli.phar

为下载的文件添加可执行权限:

sudo chmod +x wp-cli.phar

移动到bin目录:

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

查看版本信息:

$ wp --info
OS: Linux 4.13.0-32-generic #35~16.04.1-Ubuntu SMP Thu Jan 25 10:13:43 UTC 2018 x86_64
Shell:  /bin/bash
PHP binary: /usr/bin/php7.0
PHP version:    7.0.22-0ubuntu0.16.04.1
php.ini used:   /etc/php/7.0/cli/php.ini
WP-CLI root dir:    phar://wp-cli.phar
WP-CLI vendor dir:  phar://wp-cli.phar/vendor
WP_CLI phar path:   /var/www/html/justcode
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 1.5.0

wp-cli-720x432.png

使用

输入 wp 命令,回车以后,你会看到所有可以执行的命令,或者查看 wp-cli 官方提供的命令列表。下面我们可以试一下用 wp-cli 去升级 WordPress,先进入到你的 WordPress 网站的目录下面。然后输入:

wp core update

返回:

正在从http://cn.wordpress.org/wordpress-4.9.4-zh_CN.zip下载更新文件...
正在解压缩升级文件...
Success: WordPress updated successfully.

wp-cli.png

安装wordpress并配置数据库

cd /var/www/wordpress

下载 WordPress 中文版最后正式版4.9.4:

sudo wp core download --version=4.9.4 --locale=zh_CN

配置MySQL数据库:

mysql -u root -p
Enter password:

创建一个新用户和一个数据库:

mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
mysql> create user w_p@localhost identified by 'w_p@';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on wordpress.* to w_p@localhost identified by 'w_p@';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> \q
Bye

创建了一个新用户w_p和一个数据库wordpress。

创建wordpress配置文件wp-config.php:

sudo wp --allow-root core config --dbname=wordpress --dbuser=w_p --dbpass=w_p@

使用wp core install安装wordpress:

sudo wp --allow-root core install --url=localhost --title=websiteName --admin_user=admin --admin_password=admin --admin_email=[email protected]

WP-CLI:使用命令行工具控制 WordPress, 命令行安装,更新,配置 WordPress

wp-cli.jpg

使用WP-CLI管理wordpress主题

搜索wordpress主题:

wp theme search THEME_NAME

安装wordpress主题:

wp theme install THEME_NAME

激活wordpress主题:

wp theme activate THEME_NAME

列出所有安装的主题

wp theme list

使用WP-CLI管理wordpress插件

搜索wordpress插件:

wp plugin search PLUGIN_NAME

安装wordpress插件:

wp plugin install PLUGIN_NAME

激活wordpress插件:

wp plugin activate PLUGIN_NAME

列出安装的插件:

wp plugin list

获取插件状态:

wp plugin status

更新插件:

wp plugin update --all
或者
wp plugin update <plugin>

其他:删除,启用,停用

  • Activate plugin
$ wp plugin activate hello
Plugin 'hello' activated.
Success: Activated 1 of 1 plugins.
  • Deactivate plugin
$ wp plugin deactivate hello
Plugin 'hello' deactivated.
Success: Deactivated 1 of 1 plugins.
  • Delete plugin
$ wp plugin delete hello
Deleted 'hello' plugin.
Success: Deleted 1 of 1 plugins.
  • Install the latest version from wordpress.org and activate
$ wp plugin install bbpress --activate
Installing bbPress (2.5.9)
Downloading install package from https://downloads.wordpress.org/plugin/bbpress.2.5.9.zip...
Using cached file '/home/vagrant/.wp-cli/cache/plugin/bbpress-2.5.9.zip'...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Activating 'bbpress'...
Plugin 'bbpress' activated.
Success: Installed 1 of 1 plugins.

SUBCOMMANDS

NameDescription
wp plugin activateActivates one or more plugins.
wp plugin deactivateDeactivates one or more plugins.
wp plugin deleteDeletes plugin files without deactivating or uninstalling.
wp plugin getGets details about an installed plugin.
wp plugin installInstalls one or more plugins.
wp plugin is-installedChecks if a given plugin is installed.
wp plugin listGets a list of plugins.
wp plugin pathGets the path to a plugin or to the plugin directory.
wp plugin searchSearches the WordPress.org plugin directory.
wp plugin statusReveals the status of one or all plugins.
wp plugin toggleToggles a plugin’s activation state.
wp plugin uninstallUninstalls one or more plugins.
wp plugin updateUpdates one or more plugins.
wp plugin verify-checksumsVerifies plugin files against WordPress.org’s checksums.

使用WP-CLI管理translates 翻译

wp language core update

wp language core <command>

  • Install the Dutch core language pack.
wp language core install nl_NL
Success: Language installed.
  • Activate the Dutch core language pack.
wp language core activate nl_NL
Success: Language activated.
  • Uninstall the Dutch core language pack.
wp language core uninstall nl_NL
Success: Language uninstalled.
  • List installed core language packages.
wp language core list --status=installed
+----------+--------------+-------------+-----------+-----------+---------------------+
| language | english_name | native_name | status    | update    | updated             |
+----------+--------------+-------------+-----------+-----------+---------------------+
| nl_NL    | Dutch        | Nederlands  | installed | available | 2016-05-13 08:12:50 |
+----------+--------------+-------------+-----------+-----------+---------------------+

SUBCOMMANDS

NameDescription
wp language core activateActivates a given language.
wp language core installInstalls a given language.
wp language core listLists all available languages.
wp language core uninstallUninstalls a given language.
wp language core updateUpdates installed languages.

管理wordpress数据库

  • 连接数据库:
wp db cli
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 50
Server version: 5.5.46-0ubuntu0.14.04.2 (Ubuntu)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
  • 显示数据库中的表:

wp db tables

wp_users
wp_usermeta
wp_posts
wp_comments
wp_links
wp_options
wp_postmeta
wp_terms
wp_term_taxonomy
wp_term_relationships
wp_termmeta
wp_commentmeta
  • 把数据库导出到.sql文件,通常用来备份:

wp db export ~/backup.sql

导入sql文件到数据库:

wp db import ~/backup.sql

执行SQL查询语句:

wp db query "SELECT * FROM wp_users"

WP-CLI还有很多其它命令,例如,管理wordpress用户,post,菜单,widget。使用wp –help查看帮助。

WP-CLI Commands

CommandDescription
wp adminOpen /wp-admin/ in a browser.
wp cacheAdds, removes, fetches, and flushes the WP Object Cache object.
wp capAdds, removes, and lists capabilities of a user role.
wp cliReview current WP-CLI info, check for updates, or see defined aliases.
wp commentCreates, updates, deletes, and moderates comments.
wp configGenerates and reads the wp-config.php file.
wp coreDownloads, installs, updates, and manages a WordPress installation.
wp cronTests, runs, and deletes WP-Cron events; manages WP-Cron schedules.
wp dbPerforms basic database operations using credentials stored in wp-config.php.
wp dist-archiveCreate a distribution archive based on a project’s .distignore file.
wp embedInspects oEmbed providers, clears embed cache, and more.
wp evalExecutes arbitrary PHP code.
wp eval-fileLoads and executes a PHP file.
wp exportExports WordPress content to a WXR file.
wp findFind WordPress installations on the filesystem.
wp helpGet help on WP-CLI, or on a specific command.
wp importImports content from a given WXR file.
wp languageInstalls, activates, and manages language packs.
wp mediaImports files as attachments, regenerates thumbnails, or lists registered image sizes.
wp menuLists, creates, assigns, and deletes the active theme’s navigation menus.
wp networkPerform network-wide operations.
wp optionRetrieves and sets site options, including plugin and WordPress settings.
wp packageLists, installs, and removes WP-CLI packages.
wp pluginManages plugins, including installs, activations, and updates.
wp postManages posts, content, and meta.
wp post-typeRetrieves details on the site’s registered post types.
wp profile
wp rewriteLists or flushes the site’s rewrite rules, updates the permalink structure.
wp roleManages user roles, including creating new roles and resetting to defaults.
wp scaffoldGenerates code for post types, taxonomies, plugins, child themes, etc.
wp search-replaceSearches/replaces strings in the database.
wp serverLaunches PHP’s built-in web server for a specific WordPress installation.
wp shellOpens an interactive PHP console for running and testing PHP code.
wp sidebarLists registered sidebars.
wp siteCreates, deletes, empties, moderates, and lists one or more sites on a multisite installation.
wp super-adminLists, adds, or removes super admin users on a multisite installation.
wp taxonomyRetrieves information about registered taxonomies.
wp termManages taxonomy terms and term meta, with create, delete, and list commands.
wp themeManages themes, including installs, activations, and updates.
wp transientAdds, gets, and deletes entries in the WordPress Transient Cache.
wp userManages users, along with their roles, capabilities, and meta.
wp widgetManages widgets, including adding and moving them within sidebars.
最后修改:2018 年 08 月 03 日
如果觉得我的文章对你有用,请随意赞赏