Using the @wordpress/env package for local development

,

Every time I start a new small WordPress plugin I run into the same question. “What’s the fastest / easiest way to get working on the actually project?” And for a while now the answer for this question for me personally has been @wordpress/env.

The @wordpress/env package can get installed through NPM and offers an easy command line interface to manage a Docker Container running WordPress. To add it to a project, like a WordPress Theme or Plugin, you need to run npm install @wordpress/env --save-dev in the directory of the theme / plugin. From there you’ll need to add a new script to the scripts section of the package.json file. This script can be named whatever you want and it runs `wp-env`.

{
    "scripts": {
        "env": "wp-env"
    }
}

The last ting that needs to be done is creating a file called .wp-env.json. This file is the actual config file where we can configure the WordPress installation. We can tell it what plugins and themes we want to install and even configure globals in the wp-config.php file. All of the available configurations can be found here: https://www.npmjs.com/package/@wordpress/env#wp-envjson.

Most of the time I want to just have the WordPress install run one single plugin, the plugin that I am developing. In that cause the .wp-env.json file can look like this:

{
    "plugins": [ "." ]
}

This makes maps the current directory as a WordPress Plugin inside the Docker container.

And that’s it. All that is left from here is running npm run env start (replace env with whatever you called the script) and you will find a message in the terminal pointing you to the port on localhost where the WordPress installation is running.

Happy developing 🙂

Links:

Leave a Reply

Your email address will not be published. Required fields are marked *