WordPress Local Development Like a Pro Using @wordpress/env

@wordpress/env is a powerful tool designed to streamline the WordPress development process. This utility enables developers to rapidly create consistent, reproducible WordPress environments for building and testing plugins and themes. Key benefits of @wordpress/env include:

  1. Efficient setup: Create a fully functional WordPress environment in minutes.
  2. Environment consistency: Maintain uniform development environments across teams and projects.
  3. Flexible configuration testing: Easily switch between WordPress and PHP versions.
  4. Seamless workflow integration: Compatible with existing development tools and processes.

In this guide, we’ll walk you through the fundamentals of @wordpress/env and demonstrate how it can optimize your WordPress development workflow.

1. Prerequisites

Before we dive in, make sure you have the following installed:

  • Docker
  • Node.js
  • Git

2.1 Global setup/installation

You can install @wordpress/env globally using npm:

npm -g i @wordpress/env

2.2 Local Setup with package.json

If you prefer to use @wordpress/env as a local package in your project, follow these steps:

  1. Install @wordpress/env as a dev dependency:
npm install @wordpress/env --save-dev
  1. Add scripts to your package.json file:
{
  "scripts": {
    "start": "wp-env start",
    "stop": "wp-env stop",
    "destroy": "wp-env destroy"
  }
}
  1. Now you can use npm run commands to manage your environment
npm run start
npm run stop
npm run destroy

3. Quick Start

To get started, navigate to your plugin or theme directory and run:

wp-env start

This will set up a local WordPress environment at http://localhost:8888 (username: admin, password: password).

4. Configuration with .wp-env.json

Create a .wp-env.json file in your project root to customize your environment. Here’s a basic example:

{
  "core": null,
  "plugins": [ ".", "gutenberg", "woocommerce" ],
  "themes": [ "./my-custom-theme" ]
}

This configuration uses the latest WordPress version, installs your current directory as a plugin, and installs a theme from the “my-custom-theme” directory.

You can also use any plugins and themes from the WordPress directory; you can add the theme or plugin slug to the plugins and themes arrays.

5. Common Commands

  • Start the environment: wp-env start
  • Stop the environment: wp-env stop
  • Run WP-CLI commands: wp-env run cli wp user list
  • Access the environment: http://localhost:8888 (default)

6. Advanced Usage

  • Custom ports: Set "port": 3000 in your .wp-env.json
  • Specific PHP version: Set "phpVersion": "7.4"
  • Map additional directories: Use the "mappings" option. (Mapping of WordPress directories to local directories to be mounted in the WordPress instance. )
  • Map wp-config.php constants to their desired values, use "configs" option

7. Troubleshooting

If you encounter issues:

  1. Check that Docker is running
  2. Verify the port numbers
  3. Try wp-env start --update to download updates
  4. Restart Docker
  5. As a last resort, use wp-env destroy and start fresh

Further read:

Conclusion

@wordpress/env simplifies setting up a local WordPress development environment. With its easy-to-use commands and flexible configuration options, you can focus on building great plugins and themes without worrying about complex server setups.

Happy coding!

15 Likes

দারুণ লেখা। রেডিমেড সলুশন ব্যবহার না করে এভাবে ব্যবহার করলে অনেক ভালো হবে। পরবর্তীতে কাজে দেবে এইসব অভিজ্ঞতাগুলা।

3 Likes

Thanks sharing! Keep it up, Sir.

1 Like

দারুণ। সবার এটা ব্যাবহার করা উচিত।

1 Like

উইন্ডোজে ডকার ইউজ করা অনেক প্যারা বাট ফাইন ইন লিনাক্স