Developing a WordPress Plugin Using WP-Env
Before we could develop on wp-env there was Xammp, Mamp, and Vagrant. Each one is great for it’s time. Those tools allowed us developers to create a nice little environment to run WordPress locally. It’s known a as a LAMP stack (Linux + Apache + MySQL + PHP).
Then came along Docker and pretty much created an entire new standard and an ecosystem grew around Docker. Whoosh! That was the sound of Docker taking over. It made everything substantially easier and consistent.
Wp-env is a WordPress specific environment helper. Once you have Docker (or Docker Compose) and wp-env installed, all you need to do is type “wp-env start” in the terminal. Check out what happens in Docker. You’ll see two versions of WordPress running with MySQL databases. Just open a browser and point it to localhost:8888 for the development site, or localhost:8889 for the test site. I’m still not completely sure what the heck the difference is. They both pull files from the same directories, but they use different databases.
Why not just use a Docker Image for WordPress and another for MySQL? Sure. You can do that… but you’ll need to manage the docker settings yourself. And less time spent configuring tools, the more time you can spend building something awesome.
How to configure Wp-env? Create a file, in the directory where you are developing called .wp-env.json. You can put all your settings in there. You can also easily see the configuration files generated by wp-env by adding the –debug flag after the start command. Wp-env will print the configuration file and docker-compose file to the screen as well.
PHP debugging is made extremely easy with wp-env. It’s probably my single favorite feature. Just add the –xdebug flag and debugging is active. Of course you need to set up XDebug in your editor too. Debugging is essential when developing.
Here is how I set up my environment for developing a new plugin. Here is my default wp-env config file. And I also add a couple of lines to my hosts file. That way wp-cron works correctly.