Using composer for dependency management in plugindevelopment

There are still very few WP plugins/themes that are being developed Composer-first. You can take a look at Laps case study (one of mine) for practical example.

Essentially if you plan for plugin to be publicly distributed you need to take care of both cases – running it as part of whole-site Composer stack and running it standalone.

In practice this usually means looking and conditionally loading autoloader if its present (with rest of vendor stuff) inside plugin’s directory. Along the lines of:

if ( file_exists( __DIR__ . '/vendor/autoload.php' ) )
    require __DIR__ . '/vendor/autoload.php';

This doesn’t address issue of multiple plugin with duplicated dependencies, however it’s no worse than exactly same thing happening without Composer involved. Simply put WordPress has no native dependency management, so the only way to reliably handle it is introduce it externally – such as managing whole WordPress stack with Composer.

Leave a Comment