Bootstrap a WordPress Plugin
A WordPress plugin needs one predictable place to construct its container, load configuration, and register service providers. Keep that work in a small application composition root so feature classes receive fully configured dependencies.
Create the application configuration
Section titled “Create the application configuration”The root config.php maps environment values into the structure providers consume:
Compose the application in App
Section titled “Compose the application in App”Create src/App.php. The application object binds shared values before registering providers, and its provider list makes startup order visible from one file:
Add infrastructure providers and top-level feature providers directly to PROVIDERS as application features are introduced. Keep cross-feature dependencies and their registration order visible in this composition root.
A large feature may expose one composition provider that registers its own internal providers. That provider should do only that: it must not also register service definitions, configuration, or hooks.
Create the application helper
Section titled “Create the application helper”Create src/functions.php. The helper supplies the application dependencies on first use, and App::instance() returns the same application for the remainder of the request:
Calls elsewhere in the plugin can use your_plugin()->container() when WordPress invokes a callback that cannot receive constructor dependencies, such as activation and deactivation hooks. Application services should continue to use constructor injection.
Autoload the application with Composer
Section titled “Autoload the application with Composer”In the root composer.json, map the plugin namespace to src/ and autoload the application helper:
Regenerate Composer’s autoloader after changing the mapping:
Start the application from the plugin entrypoint
Section titled “Start the application from the plugin entrypoint”The root your-plugin.php can now load Composer and defer application startup to an appropriate WordPress hook:
Choose the hook and priority according to when the plugin’s integrations must become available. The entrypoint should not contain feature bindings or business behavior.
With this structure, WordPress starts one application, the application prepares one container, and providers compose each feature without spreading bootstrap logic throughout the plugin.
Continue
Section titled “Continue”Register service providers for the plugin’s application features.