Git submodules for a plugin shared across client sites
An agency plugin used in eight client sites tends to end up as eight copies, each with its own small fixes. A Git submodule keeps the plugin in its own repository and puts it into each site repository as a pointer to one commit. A fix is made once, and each site moves to the new version deliberately.
Problem: The same authentication plugin is copied into eight site repositories, and a security fix has to be applied eight times by hand.
Solution: Move the plugin into its own repository, add it to each site with git submodule add under wp-content/plugins/, clone sites with --recurse-submodules, and update a site with git submodule update --remote followed by a normal commit of the new pointer.
Add the submodule
cd client-site git submodule add git@github.com:agency/ha-auth.git wp-content/plugins/ha-auth git commit -m "Add ha-auth as a submodule"
Git creates .gitmodules with the URL and path, and stores in the site repository only the commit the submodule is on. The plugin’s files are not part of the site’s history.
Clone and pull
git clone --recurse-submodules git@github.com:agency/client-site.git # or, in an existing clone: git submodule update --init --recursive git config --global submodule.recurse true # pull and checkout update submodules too
Without these, a fresh clone has an empty wp-content/plugins/ha-auth folder, and WordPress reports the plugin as missing.
Update a site to a new plugin version
git submodule update --remote wp-content/plugins/ha-auth git diff --submodule # shows the commits that come in git commit -am "Update ha-auth to 1.4.2" git push
--remote moves the submodule to the latest commit of its default branch (or of the branch set in .gitmodules). The site keeps that exact commit until the next update, so each site is updated and tested on its own schedule. To pin a release, check out its tag inside the submodule folder (git -C wp-content/plugins/ha-auth checkout v1.4.2) and commit the pointer.
Change the plugin from inside a site
cd wp-content/plugins/ha-auth git switch main # edit, commit, push as in any repository cd ../../.. git commit -am "Use ha-auth with the new login check"
A submodule is usually in “detached HEAD” state after an update. Switch to a branch before committing, or the commits are easy to lose.
Deployment
The deploy process must fetch the submodules too: git pull --recurse-submodules on the server, or submodules: recursive in the checkout step of a CI pipeline. A deploy key of the site repository usually has no access to the plugin repository; give the server a key that may read both.
NOTE: Submodules suit a few shared plugins in Git-managed sites. For many shared packages, Composer with a private repository or a private package registry handles versions and dependencies better; see Composer in WordPress development.