WP-CLIを使えば、これまで WordPressの管理画面から ブラウザで操作して設定していたことが、コマンドラインで実行できるようになります。自分でスクリプトを書いて、一括で複数のサイトに適用するといったことが可能になり、設定時間の短縮にもつながります。
- WP-CLI v0.25.0
Contents
wp
コマンドのインストール方法 (mac or linux)
1 2 3 4 5 |
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar $ php wp-cli.phar --info $ chmod +x wp-cli.phar $ sudo mv wp-cli.phar /usr/local/bin/wp |
サブコマンド一覧表示
wpコマンドでは、サブコマンドを使って、さまざまなリソースを操作することができます。
どのようなサブコマンドがあるかは wp
と入力してエンターすることで、一覧表示させることができます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
$ wp NAME wp DESCRIPTION Manage WordPress through the command-line. SYNOPSIS wp <command> SUBCOMMANDS cache Manage the object cache. cap Manage user capabilities. cli Get information about WP-CLI itself. comment Manage comments. core Download, install, update and otherwise manage WordPress proper. cron Manage WP-Cron events and schedules. db Perform basic database operations. eval Execute arbitrary PHP code. eval-file Load and execute a PHP file. export Export content to a WXR file. help Get help on WP-CLI, or on a specific command. import Import content from a WXR file. media Manage attachments. menu List, create, assign, and delete menus. network option Manage options. package Manage WP-CLI packages. plugin Manage plugins. post Manage posts. post-type Manage post types. rewrite Manage rewrite rules. role Manage user roles. scaffold Generate code for post types, taxonomies, etc. search-replace Search/replace strings in the database. server Launch PHP's built-in web server for this specific WordPress installation. shell Interactive PHP console. sidebar Manage sidebars. site Perform site-wide operations. super-admin List, add, and remove super admins from a network. taxonomy Manage taxonomies. term Manage terms. theme Manage themes. transient Manage transients. user Manage users. widget Manage sidebar widgets. ...省略... |
このように、とても多くの操作ができることがわかります。
サブコマンドの使い方を表示する (help)
例えば、 media サブコマンドの使い方を見る場合:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
$ wp help media NAME wp media DESCRIPTION Manage attachments. SYNOPSIS wp media <command> SUBCOMMANDS import Create attachments from local files or from URLs. regenerate Regenerate thumbnail(s). EXAMPLES # Re-generate all thumbnails, without confirmation. $ wp media regenerate --yes Found 3 images to regenerate. 1/3 Regenerated thumbnails for "Sydney Harbor Bridge" (ID 760). 2/3 Regenerated thumbnails for "Boardwalk" (ID 757). ...省略... |
このように、コマンドの使い方や書式を見ることができますー。
search-replace: DBのデータの検索と置換
search-replaceの書式: ‘検索文字列’ を ‘置換文字列’ で置き換える場合
1 2 |
$ wp search-replace '検索文字列' '置換文字列' |
skip-columns: DBのすべてのテーブルに対して、URLを置換する。ただし、guidは置換の対象外とする。
1 2 |
$ wp search-replace 'http://example.dev' 'http://example.com' --skip-columns=guid |
exportオプション: DBの内容をSQLファイルに書き出す。その際、指定したURLを書き換える。
1 2 |
$ wp search-replace 'http://example.dev' 'http://example.com' --skip-columns=guid --export=replaced.sql |
dry-runオプション: 実際にはDBを変更したくないが、結果がどうなるかを確認した時は dry-run を指定すると良いです
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
$ wp search-replace 'http:' 'https:' wp_posts wp_postmeta wp_terms --dry-run +-------------+-----------------------+--------------+------+ | Table | Column | Replacements | Type | +-------------+-----------------------+--------------+------+ | wp_posts | post_content | 48 | SQL | | wp_posts | post_title | 0 | SQL | | wp_posts | post_excerpt | 0 | SQL | | wp_posts | post_status | 0 | SQL | | wp_posts | comment_status | 0 | SQL | | wp_posts | ping_status | 0 | SQL | | wp_posts | post_password | 0 | SQL | | wp_posts | post_name | 0 | SQL | | wp_posts | to_ping | 0 | SQL | | wp_posts | pinged | 0 | SQL | | wp_posts | post_content_filtered | 43 | SQL | | wp_posts | guid | 109 | SQL | | wp_posts | post_type | 0 | SQL | | wp_posts | post_mime_type | 0 | SQL | | wp_postmeta | meta_key | 0 | SQL | | wp_postmeta | meta_value | 2 | PHP | | wp_terms | name | 0 | SQL | | wp_terms | slug | 0 | SQL | +-------------+-----------------------+--------------+------+ Success: 202 replacements to be made. |
他にも様々なオプションがあるので、以下を参考にしてみてください。
wp search-replace | WP-CLI
テーマ
テーマの一覧をみる
1 2 |
$ wp theme list |
テーマの検索/インストール/削除
プラグイン
プラグインの検索/インストール/有効化/無効化/削除
scaffold
scaffold(スキャッフォールド) とは 「足場」 のことで、よく書くコードを生成してくれるコマンドです。
新規テーマの雛形作成
1 2 |
$ wp scaffold _s sample-theme --theme_name="Sample Theme" --author="John Doe" |
子テーマの雛形作成
1 2 |
$ wp scaffold child-theme sample-theme --parent_theme=twentysixteen |
プラグインのスターターコードを生成
1 2 |
$ wp scaffold plugin |
プラグインの単体テストの足場を作る
1 2 |
$ wp scaffold plugin-tests |
テーマの単体テストの足場を作る (v0.26.0以上)
1 2 |
$ wp scaffold theme-tests |
db
WordPress データベースの検索と置換
1 2 3 4 5 6 7 8 9 10 11 12 |
check DBの現在の状態をチェックし、結果を表示します。 cli Open a MySQL console using credentials from wp-config.php create Create a new database. drop Delete the existing database. export Exports the database to a file or to STDOUT. import Import a database from a file or from STDIN. optimize Optimize the database. query Execute a SQL query against the database. repair Repair the database. reset Remove all tables from the database. tables List the database tables. |
terms
カスタム投稿タイプとカスタムタクソノミーの設定
shell
WordPress関数や自作の関数の動作確認をしたい時に便利です。
以下のコマンドを実行すると、shellが起動します。
1 2 |
$ wp shell |
get_posts関数を実行してみる:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
wp> get_posts(['posts_per_page'=>1]); => array(1) { [0]=> object(WP_Post)#6424 (24) { ["ID"]=> int(81) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2016-11-10 19:00:13" ["post_date_gmt"]=> string(19) "2016-11-10 10:00:13" ... snip ... |
WordPressの設定を確認することもできます:
1 2 3 4 |
wp> bloginfo('home_url'); wpken.net |
SQLを実行することもできます:
1 2 3 |
wp> global $wpdb; wp> $wpdb->get_results('select * from wp_posts limit 1'); |
user
管理者ユーザーを作成
1 2 |
$ wp user create username email@example.com --role=administrator --user_pass=secret |
media
サムネを再生成
サムネールのサイズ定義を追加したときなどに、再度サムネールを生成するのに便利。
1 2 |
$ wp media regenerate |
cache
キャッシュ削除
1 2 |
$ wp cache flush |
W3 Total Cacheプラグインのキャッシュを削除
いくつかのWordPressプラグインには、WP-CLIから操作するためのインターフェースが用意されていることがあります。
その場合は、wpコマンドからその機能を呼び出すことができます。以下はW3 Total Cacheのキャッシュを削除する場合です。
1 2 |
$ wp w3-total-cache flush |
WP-CLIに対応しているプラグインは こちらに一例が掲載されています Integrated tools | WP-CLI 。
package
wp-cli に機能を追加するためのコマンドが package
です。
パッケージを管理するためのサブコマンドが揃っています。
- browse インストール可能なパッケージを検索し、表示します
- install WP-CLIパッケージをインストールします
- list インストール済みのパッケージをリストアップします
- path パッケージをインストールするパスを表示します
- uninstall パッケージをアンインストールします
インストール方法
例えば hook コマンドをインストールするには以下のようにします:
安定版をインストールする場合は以下のようにします:
1 2 |
$ wp package install wp-cli/server-command:@stable |
githubからインストール:
1 2 |
$ wp package install git@github.com:runcommand/hook.git |
ダウンロードしたzipファイルからインストール:
1 2 |
$ wp package install google-sitemap-generator-cli.zip |
詳しくは公式サイトを御覧ください:
wp package install | WP-CLI
WP-CLIの今後ロードマップ
- 4.7対応
- REST API 対応
No comment yet, add your voice below!