Using composer for dependency management in plugindevelopment

I’m new to wordpress plugin development. Normally I would use composer to add thirdparty libs to my code, but I couldn’t find any wordpress plugin on github that uses composer for that.

I read about wpackagist.org and the way composer can be used to manage dependencies between wordpress plugins. In this case there would be one master composer.json with all infos.

But what if I want to develop a single plugin using composer? Would I simply include the autoload in the “plugin root”-file? But what if other plugins would do it the same way and would bring own autloaders and in the worst case have the same dependencies in different versions?

Is there a best practice for integrating lib-dependencies into wordpress plugins?

1
1

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