Plugin: install dependencies / dump autoload

A WordPress plugin is installed as a composer package using composer.
The plugin itself also requires composer packages on its own.
Can composer automatically install the required packages, too?

It can. What have you tried?

1 Like

composer install – but no vendor/ folder was installed.

$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Package operations: 1 install, 0 updates, 0 removals
- Installing my/test-plugin (v2.1.0): Cloning 2ca3493020 from cache
Generating autoload files

composer.json of plugin:

{
    "name": "my/test-plugin",
    "description": "Test",
    "keywords": [
        "WordPress", "plugin", "test"
    ],
    "license": "MIT",
    "type": "wordpress-plugin",
    "require": {
        "php": ">=5.4.0",
        "composer/installers": "~1.0",
        "league/uri": "^5.2"
    },
    "minimum-stability": "dev"
}

"league/uri": "^5.2" should be installed + autoloader dumped.

Plugin has currently issues requiring the composer autoloader:

( ! ) Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in [...]

I had to cwd into the plugin directory and run composer install manually.

Apparently composer autoloading is handled differently when the plugin is installed into WordPress compared to standalone development, there is a site/vendor directory.

I added a condition for requiring the autoload.php file
and now the use seem to work correctly.

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