KON BLOG

6/19/2021

wp-envでWordPress開発環境構築

  • WordPress

Dockerを起動する

wp-envはDocker上で動かすため、Dockerを起動しておきます。

wp-envのモジュールをインストール

まずはnpmを初期化します。

npm init

以下のコマンドを実行して、wp-envをインストールします。

npm i @wordpress/env --save-dev

package.jsonを編集する

wp-envの実行用コマンドをscriptsに追加

"scripts": {
    "start":"wp-env start",
    "stop": "wp-env stop",
    "restart": "wp-env start —update",
},

次のコマンドでwp-envを実行してWordPressを起動

npm run start

終了するときは

npm run stop

テーマやプラグインを追加した時に再起動する場合は

npm run restart

成功するとデフォルトテーマ「Twenty Twenty-One」が設定された状態で起動します

テーマやプラグインを追加する

.wp-env.jsonファイルを作成し、テーマやプラグインを設定する

{
  "themes": [ "./themes/test-theme" ],
  "plugins": [ "./test-block" ]
}

追加したあとはwp-envを再起動

npm run restart


日本語版パッケージをデフォルトにする

.wp-env.jsonに以下を追記する

{
  "core": "https://ja.wordpress.org/latest-ja.zip"
}

導入時に発生したエラー(原因は特定できず)

実は1ヶ月ほど前に導入にチャレンジした際に、下記のようなエラーが表示されWordPressが起動しませんでした。
その後、再チャレンジした際には問題なく起動しました。念のため備忘録として残しておきます。
(直前にnpmをアップデートしたため、これが原因だった?)

> study_wp_demo@1.0.0 wp-env 
> wp-env "start"
⚠ Warning: could not find a .wp-env.json configuration file and could not determine if '/Users/.......' is a WordPress installation, a plugin, or a theme.
✖ Error while running docker-compose command.
Creating c5c6f7f2ce9d2e8f0b50eca336daa42f_cli_run ... 
Creating c5c6f7f2ce9d2e8f0b50eca336daa42f_cli_run ... done
mysqlcheck: Got error: 1045: Access denied for user 'username_here'@'172.19.0.5' (using password: YES) when trying to connect
2
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! study_wp_demo@1.0.0 wp-env: `wp-env "start"`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the study_wp_demo@1.0.0 wp-env script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:

関連リンク