Git Monorepo Strategy for WordPress Plugin Development

A monorepo for several WordPress plugins

An agency with five plugins and a shared library usually has six repositories. A change in the library means a release of the library, then a Composer update and a pull request in every plugin, and the integration tests run only after all of that. A monorepo puts all of them into one repository: one pull request changes the library and every plugin that uses it, and CI tests them together.

Problem: A shared library for license checks and admin notices lives in its own repository and is required by four plugins with Composer. A breaking change in the library was released, two plugins were updated, two were forgotten, and customers got a fatal error after a plugin update.

Solution: Move the plugins and the library into one repository with a packages/ folder, link the library into the plugins with a Composer path repository during development, prefix the library’s namespace in each plugin’s release build so that two plugins with different library versions cannot collide on a site, and build each plugin’s ZIP separately in CI.

The layout

agency-plugins/
├── composer.json            (dev tools for all: phpunit, phpcs, phpstan)
├── packages/
│   └── ha-common/           shared library, namespace HaCommon\
│       ├── composer.json
│       └── src/
├── plugins/
│   ├── ha-forms/
│   │   ├── composer.json
│   │   └── ha-forms.php
│   └── ha-booking/
└── .github/workflows/

Link the library with a path repository

{
    "name": "agency/ha-forms",
    "require": {
        "agency/ha-common": "@dev"
    },
    "repositories": [
        { "type": "path", "url": "../../packages/ha-common", "options": { "symlink": true } }
    ],
    "autoload": { "psr-4": { "HaForms\\": "src/" } }
}

composer install in plugins/ha-forms symlinks the library into its vendor folder. A change in packages/ha-common is visible in every plugin at once, and the tests of all plugins run against the changed library in the same pull request. There is no library version to release and no plugin that is forgotten.

The problem every WordPress plugin has with shared code

On a customer’s site, ha-forms and ha-booking are installed together. If both ship HaCommon\Notice in their vendor folder, PHP loads whichever plugin’s autoloader is asked first, and the other plugin runs with a class from a different version. That is the original reason for many fatal errors with shared libraries, and a monorepo does not solve it by itself.

The usual fix is to rewrite the namespace in each plugin’s build, so that every plugin carries its own copy:

# in CI, per plugin, after composer install --no-dev
vendor/bin/php-scoper add-prefix --prefix='HaForms\Vendor' --output-dir=build/ha-forms --force
composer dump-autoload --working-dir=build/ha-forms --classmap-authoritative

After the build, ha-forms uses HaForms\Vendor\HaCommon\Notice and ha-booking uses HaBooking\Vendor\HaCommon\Notice. Tools for this are PHP-Scoper and Strauss; WordPress functions and classes must be excluded from prefixing in their configuration (both have WordPress presets or exclusion lists).

CI per plugin

# .github/workflows/plugins.yml (excerpt)
jobs:
  test:
    strategy:
      matrix:
        plugin: [ ha-forms, ha-booking ]
    steps:
      - uses: actions/checkout@v4
      - run: composer install --working-dir=plugins/${{ matrix.plugin }}
      - run: vendor/bin/phpunit -c plugins/${{ matrix.plugin }}/phpunit.xml.dist

A change in packages/ha-common tests all plugins; with path filters (on.pull_request.paths), a change in only one plugin can skip the others. Release ZIPs are built per plugin from the scoped build/ folder. The basic pipeline: GitHub Actions CI/CD for WordPress plugins.

When a monorepo is the wrong choice

Plugins that are published on WordPress.org need their own SVN repository for releases anyway, and plugins maintained by different teams with different release cycles gain little. A monorepo pays off when the plugins share code and are released by the same people. The alternative for a few shared files without Composer is a git submodule: git submodules for shared plugins.

NOTE: Large monorepos make every clone and every CI checkout bigger. A shallow clone (actions/checkout does fetch-depth: 1 by default) keeps CI fast; for developers, a partial clone (git clone --filter=blob:none) downloads file contents only when they are needed.