WordPress Local Development with wp-env: Docker-Powered Zero-Config Setup

wp-env: a local WordPress for plugin and block development in one command

For developing a plugin or a block, a full local stack is often more than needed. @wordpress/env (wp-env) is the tool the WordPress project uses itself: one .wp-env.json in the plugin folder, one command, and Docker runs WordPress with the plugin mounted, a test database, WP-CLI and optionally Xdebug. Every developer and the CI pipeline get the same environment.

Problem: Contributors to a block plugin use different local setups, and a bug report says “works on my machine”. Setting up PHPUnit with the WordPress test suite takes new contributors an afternoon.

Solution: Add @wordpress/env as a dev dependency, describe WordPress version, PHP version, plugins and settings in .wp-env.json, start it with npx wp-env start, run WP-CLI and PHPUnit inside the containers with wp-env run, and use the separate tests instance for automated tests.

Configuration

{
    "core": "WordPress/WordPress#6.9",
    "phpVersion": "8.3",
    "plugins": [ ".", "https://downloads.wordpress.org/plugin/query-monitor.zip" ],
    "themes": [ "WordPress/twentytwentyfive" ],
    "config": {
        "WP_DEBUG": true,
        "SCRIPT_DEBUG": true
    },
    "mappings": {
        "wp-content/mu-plugins/dev-tweaks.php": "./dev/dev-tweaks.php"
    }
}
  • core takes a GitHub reference (a version branch or tag), a ZIP URL or a local path; null means the latest release.
  • "." in plugins mounts the current folder as a plugin, so changes are live without copying.
  • config becomes constants in wp-config.php.
  • mappings mounts extra files or folders anywhere in the installation.

Daily commands

npm install --save-dev @wordpress/env
npx wp-env start                      # http://localhost:8888, admin / password
npx wp-env start --xdebug             # with Xdebug for step debugging
npx wp-env run cli wp plugin list     # WP-CLI in the CLI container
npx wp-env run cli wp option get siteurl
npx wp-env logs
npx wp-env stop
npx wp-env clean all                  # reset the databases
npx wp-env destroy                    # remove everything

wp-env starts two WordPress instances: the development site on port 8888 and a tests site on port 8889 with its own database, so automated tests never touch the data you work with. Ports and other settings can be changed in .wp-env.json or with environment variables (WP_ENV_PORT).

PHPUnit in the tests container

{
    "scripts": {
        "env": "wp-env",
        "test:php": "wp-env run tests-cli --env-cwd=wp-content/plugins/ha-plugin vendor/bin/phpunit"
    }
}

npm run test:php runs the plugin’s tests inside the tests container, where the WordPress test library is available; the plugin’s phpunit.xml.dist and bootstrap work as with wp scaffold plugin-tests: unit tests with PHPUnit and WP-CLI scaffold. In GitHub Actions, the same npx wp-env start and npm run test:php run on the Ubuntu runner, which has Docker.

wp-env or a Compose file

wp-env is optimized for developing one plugin or theme against WordPress: zero configuration, standard images, quick reset. A hand-written Docker Compose setup is better when the local stack must match a specific production server (Nginx config, MariaDB version, Redis, mail catcher): Docker Compose for local WordPress.

NOTE: wp-env needs Docker running and Node.js for the command. On Apple Silicon and Windows with WSL2 it works with Docker Desktop or alternatives that provide the Docker API. The first start downloads the images and WordPress, later starts take seconds.