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

 

如果直接想了解一键更新所有,请略过本文,直接移步到:WordPress: 一键更新所有(WordPress版本,插件,主题,翻译)

 

WP-CLI,它可以让我们在命令行工具里输入相应的命令,去控制 WordPress 的行为。比如去升级 WordPress,去安装个插件,多站点,去管理用户,评论,主题等等。这跟 Drupal 的 Drush 有点类似。在 WordPress 上面,我们就是去使用 WP-CLI 这个工具。

下面,我们还是在自己的 Linux 系统的服务器上,去安装一个 WP-CLI ,然后再用它使用命令去控制 WordPress,你也可以在本地电脑上去测试,需要使用类 Unix  的操作系统,比如 OS X, Linux, FreeBSD … 如果你使用是 Windows 操作系统,可以去安装一个 Linux 系统的虚拟机,或者,你也可以直接买一台服务器去测试一下。下面, 我会在ubuntu操作系统的服务器上测试这个 WP-CLI 工具。

需求

  • Unix-like 环境(OS X, Linux,FreeBSD,Cygwin)
  • PHP 5.3.2 或以上版本
  • WordPress 3.5.2 或以上版本

安装

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

wp core update

返回:

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

安装wordpress并配置数据库

cd /var/www/wordpress

下载wordpress:

sudo wp --allow-root  core download --version=4.9.4

配置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=test@123.com
WP-CLI:使用命令行工具控制 WordPress, 命令行安装,更新,配置 WordPress
WP-CLI:使用命令行工具控制 WordPress, 命令行安装,更新,配置 WordPress

使用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

Name Description
wp plugin activate Activates one or more plugins.
wp plugin deactivate Deactivates one or more plugins.
wp plugin delete Deletes plugin files without deactivating or uninstalling.
wp plugin get Gets details about an installed plugin.
wp plugin install Installs one or more plugins.
wp plugin is-installed Checks if a given plugin is installed.
wp plugin list Gets a list of plugins.
wp plugin path Gets the path to a plugin or to the plugin directory.
wp plugin search Searches the WordPress.org plugin directory.
wp plugin status Reveals the status of one or all plugins.
wp plugin toggle Toggles a plugin’s activation state.
wp plugin uninstall Uninstalls one or more plugins.
wp plugin update Updates one or more plugins.
wp plugin verify-checksums Verifies 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

Name Description
wp language core activate Activates a given language.
wp language core install Installs a given language.
wp language core list Lists all available languages.
wp language core uninstall Uninstalls a given language.
wp language core update Updates 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

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, installs, updates, and manages a WordPress installation.
wp cron Tests, runs, and deletes WP-Cron events; manages WP-Cron schedules.
wp db Performs basic database operations using credentials stored in wp-config.php.
wp dist-archive Create a distribution archive based on a project’s .distignore file.
wp embed Inspects oEmbed providers, clears embed cache, and more.
wp eval Executes arbitrary PHP code.
wp eval-file Loads and executes a PHP file.
wp export Exports WordPress content to a WXR file.
wp find Find WordPress installations on the filesystem.
wp help Get help on WP-CLI, or on a specific command.
wp import Imports content from a given WXR file.
wp language Installs, activates, and manages language packs.
wp media Imports files as attachments, regenerates thumbnails, or lists registered image sizes.
wp menu Lists, creates, assigns, and deletes the active theme’s navigation menus.
wp network Perform network-wide operations.
wp option Retrieves and sets site options, including plugin and WordPress settings.
wp package Lists, installs, and removes WP-CLI packages.
wp plugin Manages plugins, including installs, activations, and updates.
wp post Manages posts, content, and meta.
wp post-type Retrieves details on the site’s registered post types.
wp profile
wp rewrite Lists or flushes the site’s rewrite rules, updates the permalink structure.
wp role Manages user roles, including creating new roles and resetting to defaults.
wp scaffold Generates code for post types, taxonomies, plugins, child themes, etc.
wp search-replace Searches/replaces strings in the database.
wp server Launches PHP’s built-in web server for a specific WordPress installation.
wp shell Opens an interactive PHP console for running and testing PHP code.
wp sidebar Lists registered sidebars.
wp site Creates, deletes, empties, moderates, and lists one or more sites on a multisite installation.
wp super-admin Lists, adds, or removes super admin users on a multisite installation.
wp taxonomy Retrieves information about registered taxonomies.
wp term Manages taxonomy terms and term meta, with create, delete, and list commands.
wp theme Manages themes, including installs, activations, and updates.
wp transient Adds, gets, and deletes entries in the WordPress Transient Cache.
wp user Manages users, along with their roles, capabilities, and meta.
wp widget Manages widgets, including adding and moving them within sidebars.

 

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