WP-CLI 管理 WordPress 参考手册

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

WP-CLI 管理 WordPress 参考手册

安装

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

curl -L https://ri i ~ law.githS * # D 2 + Bub.com/wp-cli/builds/gh-p9 / @ages/phar/wp-cli% , * ` O F P.phar > wp-cld 4 ) l = Oi.phar

为下载s B q X N的文件添加可执行权限:

sudo chmod +x w @ lp-cli.ph5 b o H ~ w ~ |ar

移动到bin目录:

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

查看版本信息:

$ wp --infoX , q l R b )
OS: Linux 4.13.0-32-generic #35~16.04.1H g Z x 0-Ubuntu SMP Thu Jan 25 10:13:43 UTC 2018 x86_64
ShellQ { . x X:  /bi` I A / t _ Z ln/bash
PHP binary: /usr/bin/php7.0
PHP version:    7.T F c  0 } C ; !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-z E : I 6 ; $ U 2CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-= i K iCLI version: 1# T # 1 R ( ; *.5.0

WP-CLI 管理 WordPress 参考手册

使用

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

wp coJ c tre upL : p #date

返回:

正在从http://cn.wordpress# b C y [ r ) [.org/wordpress-4.9.4-zh_CN.zip下载更新文件...
正在解压缩升级文件...
Success: WordPress updated successfu, J  * K * *lly.

WP-CLI 管理 WordPress 参考手册

安装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 wordpP H Rress;
Query OK, 1 row affected (0.00 sec)
mysql> create7 | 4 2 Z y I  user w_p@localhost idew 4 P tntified by 'w_p@';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on wordpress.* to w_p@W 2 ( hlocalhost identified by 'w_p@';
Query OK, 0 rows affecteu * | { Y S md (0.00 sec)
mysql> flush privileges;
Query OK, 0 row= 4 $ y & : n n os 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_Q g b ep@

使用wp coa ~ @ u & F 3re install安装wordpress:

sudo wp --allow-root core install --url=loY 8 o & D s Mcalhost --title=websiteName --admin_user=admin --admin_password=admin --az P sdmin_email=test@123.com

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

WP-CLI 管理 WordPress 参考手册

使用WP-CLI管理word^ 7 ? Hpress主题

搜索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 p~ P ( H & ~ A ulugin search PLUGIN_NAME

安装wordpress插件:

wp plugin install PLUGIN_NAME

激活wordpre^ : ? ? % ess插件:

wp plugin activate PLUGIN_NAME

列出安装的插件:

wp plugin list

获取插件状态:

wp plugin status

更新插件:

wpC b L M . N % plugin update --all
或者
wU 2 z ,p plugin update <plugin>

其他:删除,启用,停用

  • Activate plugin
$ wM f # sp plugin activate hello
Plugin U k 1 9'hello' activated.
Success: Activated 1 of 1 plugins.
  • Deactivate plugin
$ wp pluginX M _ c P z z 8 deag s c - _ ` 1 Jctivate hello
Plugin 'hello' deactivated.
Success: Deactivated 1 of 1 pG 1 x o lugins.
  • Delete plugin
$ wp plugin del] b a A 4 ~ s & Wete hello
Deleted 'hello' pl& 1 _ K / 9 ougin.
Success: DelE @ @ = J ^ eeted 1 ox 4 y - n A Rf 1 pluS 8 % / sgins.
  • Install the la@ * t Q P * ` ftest version from wordpress.org and activI s ) @ W e & xate
$ wp plugin install bbpress --activate
Installing bbPra ` & _ess (2.5.9)
Downloading install package fromf N Y ( https://downloads.wordpress.org/plugin/bbpress= O s : P G 3 d ,.2.5.9.zip...
Using cached file '/home/vagrant/F Q M.wp-cli/cache/plugin/bbpress-2.5.9.zip'L M B * ( L ! ?...& = 2 D ^ w
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Activating 'bbpress'...
Plugin 'bbpress' activated.
Success: Installed 1 of 1 plugins.

SUBCOMMANDS

Name DescriptionI V V
wp plugw ( W / $ win activate Activates one or more plugins.
wp plugin deactivate Deactivates one or more pl6 Y i T x 2 S J (ugins.
wp plugin delete Deletes plugin files without deactivating or uninstalling.
wp plugin get Gets details about an installed plugin.
w$ E i Z fp plugin install Installs onez ; w H or moreA [ # * ` i G plugins.
wp plugin is-installed Checks if a given plugin? H ` Y 2 8 ( is installed.
wp plugin$ } / e s w b } D list Gets a list of plugins.
wp plugin pa] @ @th Gets the path to a plugin or to the plugin directory.
wp plc @ x Z w & r N &ugin search Searches the WordPress.org plugin directory.
wp plugin statQ ~ zus Reveals the status of one or all plugins.
wp0 V v d ~ 5 7 p ] plugin toggle TogglesZ 7 D S - H u 3 a plugin’s activation state.
wp plugin uninstall Uninstalls one or more plugins.
wp plugin upD y vdate UpdatesI v U . / S . one or more plugins.
wp plugin verify-checksums Verifies plugin files against WordPress.org’s che, P ; @ x G - [ |cksums.

使用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 instW M [ X ^ t Halled.
  • Activate the Dutch core language pack.
wp language core activate nl_d E 6 xNL
Success: Language aB 2 c * K Xctivated.
  • Uninstall the Dutch core language pack.
wp languagc - N Z 7 ne core uninstall nl_NL
Success: Langua Z Eage uninstalled.
  • List installed core language packages.
wp language core lish f ! 3 U # @ dt --status=instB m % 3alled
+----------+----p - b 4----------+-------------+-----------+-----------+---------------------+
| language1 7 M F 2 | U | english_name | native_name | status    | update    | updated             |
+----------+----d b d E S w----------+-------------+------q 3 N = , C Q C 1-----+-----------+---------------------+
| nl_n f i L 5 |NL    | Dutch        | Nederlands  | installed | availaD Q A C 9ble | 2016-05-13 08:12:50 |
+----------+--------------+---7 J $ z ^ $ ?----------+-----------+-----------+---------k W n O  H ,------------+

SUBCOMMANDS

Name Description
wp language core activate Activates a given languageg - ! 3 f G * `.
wp language core install Installs a given language.
wp languagex & B N $ core list Lists all avai Z u - B zilable languagp ( z Ees.
wp language core unin6 8 E P y 6 ~ gstall Uninstalls a given language.
wp language core update U& g k F { Fpdates installed languages.

管理wordpress数o * _ r: 6 0

  • 连接数据库:
wp db cli
Welcome top n ( j = u z the MySQL monitor.  Comman% ~ Z % C m Tds end with ; or X Y | U ; ) ~ tg.
Your MySQL connecti+ v ` l m D Von id is 50
Server version: 5.5.46+ d T { * @ 2-0ubuntu0.14.04.2 (Ubuntu)
CopyriB ! V 9 c , ught (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered tradema! j d 8 a ^ F | _rk of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective! 4 a K d g C , 0
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the currenn ! utQ = 9 : input st) ] e zatement.
mysql>
  • 显示数据库中的表:

wp db tap b Q Pbles

wp_users
wp_usermeta
wp_posts
wp_comments
wp_links
wp_options
wp_postmeta
wp_terms
w^ { Xp_term_taxonomy
wp_term_relationships
wp_termmeta
w= ] f ?  m * @ mp_commentmeta
  • 把数据库导出到.sql文件] m B 9 | . | d,通常用来备份:

wp` / o 1 p ` @ 3 db export ~/backup.sql

导入sql文件J = @到数据库:

wp db import ~/backup.sql

执行SQL查询语句:

wp db query "SELECT * FROM wp_users"

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

WP-CLI Commands

Command Description
wp admin Open /wp-admin/ in a browser.
wp cache Adds, removes, fetches, and flushes the WP Object Cache object.
wp cap Adds, removes, and lists capabilities of a user role.
wp cli Review current WP-CLI info, check for updates, or see defined aliases.
wp comment Creates, updates, deletes, and moderates comments.
wp config Generates and reads the wp-config.php file.
wp core Downloads, installsY % / ), updates3 x q 4 %, and manages a WordPress installation.
wp cron Tests, runs, and deletes WP-Cron events; manages WP-Cron sched? E Uules.
wp db Perform3 A h /s basC ` 4 Kic database operations using credentials stored in wp-config.php.
wp dist-archive Creav u * 9te a distribution archive based on a project’s .di4 8 cstignore file.
wp embed Ins! x %pects oEmbed provideB n ` M Lrs, clears embed c[ j r ~ P C Eache, a] d z ! R z + Wnd moh / M d ) 3 D q kre.
wp eval Executes arbitrary PHP code.
wp eval-file Loads and executes a PHP file.
wp export Exports WordPress content to a WXR file. h N , + p
wp find Find WordPress installations on the filesystem.
wp help Get hR } c ( x [elp on WP-5 & v x ! F Z S *CLI, or on a specific command.
wp iY ~ O o O amport Im{ k o 7 ] 4 ) 9ports content from a given WXR file.
wF S 7 _ S / s 6 vp language IF A f l % 0 K s Anstalls, activates, and manages language packs.
wp media C 9 ! o u ( Imports files as attachments, regenerates thumbnails, or lists registered image sizes.
wp menu Lists, creates, assigns, and delh 8 X D U 4 # o xetes the active theme’s n h g , . q . 1avigation menus.
wp network Perform network-wide operations.
wp option RetrievG v K .es and sets site options, including plugin and WordPress settings.
wp package Lists, installs, and remo= [ _ - Yves WP-r ) 9 k _ PCLI packages.
wp plugin Manages plugins, including installs, activations, and updates.
wp post Manages posts, content, and meta.
w2 4 O ! 3 ~p post-type Retrieve@ h 4 3 4 ;s details on the site’s registered post types.
wp profile
wp rewrite Lists or flushes the site’s rewrite rules, updates the permE a R 8 Lalink structure.
wp role Manages user roles, including creating new roles and resetting to defaults.
wp scaffold Generates code for post types, taxonomieI ] w f s qs, plugins, child themes, etc.
wp s i i 9earch-replace Searches/replaces strings in the database.
wp server Launches PHP’s built-h D u Y n & 5 Bin web server for a specific WordPress installation.
wp shell Opens an interactive PHP console for running andB 2 B testing PHP code.
wp sidebar Lists registered sidebars.
wp site Creates, deletes, empties, moderates,H T + and lists one or more sites or j n U Xn a multisite instaX f c illation.
wp super-admin Lists, adds, or removes super admin users on a multisite installation.
wp taxonomy Retrieves information about registered taxonomies.
wp term Manages taxo_ $ : l 4 3 $ , $nomy terms4 * ( R s and tt g h 0 - 1 6 y 2erm meta, with9 } Q 8 k [ z create, delete, and list commands.
wp theme Mana] P . O 9ges themes, inB p 4 6 M # ] t mcluding installs, activations, and updates.
wp transient Adds,y j S U x Q y geh % o k Zts, and deletes entries in the WordPress TranK p 7 Dsient Cache.
wp user Manages users` o J, along wiA , M 6 J :th their roles, capabilV n + P ci8 # I v ^ sties, and meta.
wp widget Man~ m T $ 2ages widge^ M cts, including adding and moving them within sidebars.