Dev Notes

Software Development Resources by David Egan.

Manage WordPress Plugins Using Composer


Composer, PHP, WordPress
David Egan

This article shows how to use Composer to manage project plugin dependencies.

The example given is for a Bedrock project. Bedrock is an improved WordPress project structure from the Roots team that manages dependencies with Composer.

Manage Plugins from The WordPress Plugin Directory

You can easily access any plugin that’s held in the WordPress Plugin Directory by means of the WordPress Packagist Composer repository.

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.

In Bedrock’s composer.json add the wp-packagist repo:

{

    "repositories": [
        {
            "type": "composer",
            "url": "https://wpackagist.org"
        }
    ]

}

Then in the same file, require the plugin:

{
    "require": {
        "php": ">=5.6",
        "composer/installers": "~1.0.12",
        "vlucas/phpdotenv": "^2.0.1",
        "johnpbloch/wordpress": "4.7.2",
        "oscarotero/env": "^1.0",
        "roots/wp-password-bcrypt": "1.0.0",
        "roots/soil": "3.7.1",
        "wpackagist-plugin/advanced-custom-fields" : "4.1.*"
    }
}

Run 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 "type": "wordpress-plugin"
  • Require the plugin in the project composer.json

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):

{
    "require": {
        "php": ">=5.6",
        "composer/installers": "~1.0.12",
        "vlucas/phpdotenv": "^2.0.1",
        "johnpbloch/wordpress": "4.7.3",
        "oscarotero/env": "^1.0",
        "roots/wp-password-bcrypt": "1.0.0",
        "carawebs/wp-custom-content": "dev-master"
    }
}

Running 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:

{
    "autoload": {
        "psr-4": {
            "Carawebs\\CustomContent\\": "src"
        }
    }
}

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, Carawebs\CustomContent\ClassName).

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 composer.json:

{
    "require": {
        "carawebs/wp-custom-content": "1.0.0"
    }
}

You can also access branches - using the branch name as a suffix on ‘dev-‘ like so:

{
    "require": {
        "carawebs/wp-custom-content": "dev-experimental-branch"
    }
}

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