This article shows how to use Composer to manage project plugin dependencies.
Manage Plugins from The WordPress Plugin Directory
WordPress Packagist mirrors the WordPress plugin and theme directories. If you add the wpackagist repository to Composer’s “repositories”, you can then require the ACF free version.
composer.json add the wp-packagist repo:
Then in the same file, require the plugin:
composer update and it should fetch the plugin.
Custom Plugins: Not in the WordPress Directory
We use a lot of custom plugins - the easiest way to manage these is to:
- Push your code to a public VCS (e.g. git) repo (GitHub, BitBucket)
- Set them up as a Composer package on Packagist
- In the plugin’s
composer.json, make sure you set
- Require the plugin in the project
Here’s an example from a Bedrock project
composer.json - this requires the
carawebs/wp-custom-content Composer package (which is a WordPress plugin that sets up custom post types from a config array):
composer update then instructs Composer to download the plugin to the appropriate directory - in the case of Bedrock,
/web/app/plugins. You can also take advantage of Composer autoloading - in the plugin
composer.json set the appropriate directory:
Assuming that you’re using Composer autoloading, you’ll now be able to access your plugin’s namespaced classes using the namespace you’ve defined (in the example above,
Access Specific Commits
You can request a specific tagged commit - so in the example above, you could create a git tag 1.0.0 and specifically require this in the project
You can also access branches - using the branch name as a suffix on ‘dev-‘ like so:
This can be useful if you want to build functionality as a pure Composer library rather than a plugin - you can keep this code in a ‘library’ branch on your git repo. This might be useful if you needed to include your functionality in another plugin.
comments powered by Disqus