I had the same issue lately. I created a custom plugin with several dependencies. My approach was to normally create composer.json
in the custom plugin, specifying its name and type: "wordpress-plugin"
. After that, I’ve also added composer/installers
as a dependency to that custom plugin. For example this is the custom plugin’s composer.json
{
"name": "lamosty/blog-helper",
"type": "wordpress-plugin",
"authors": [
{
"name": "Rastislav Lamos",
"email": "lamos.rasto@gmail.com"
}
],
"require": {
"lamosty/wp-plugin-stack": "~1.0",
"composer/installers": "1.0.21"
},
"autoload": {
"psr-4": {
"Lamosty\\Blog_Helper\\": "src/"
}
}
}
As you can see, I’m also using psr-4 autoloading feature of composer. However, you can’t simply do
require_once( 'vendor/autoload.php');
in your custom plugin’s main PHP file because if the plugin is specified as your main project’s (Bedrock) dependency, composer install
won’t run in the custom plugin’s directory so no vendor
folder is created. What composer does is to look into your custom plugin’s composer.json
, see for its dependencies (in my case
"lamosty/wp-plugin-stack": "~1.0",
"composer/installers": "1.0.21"
) and resolve these dependencies into the global vendor
folder located in the root of your Bedrock site. That’s why I’ve changed my custom plugin’s main PHP file to be as:
$autoload_path = __DIR__ . '/vendor/autoload.php';
if ( file_exists( $autoload_path ) ) {
require_once( $autoload_path );
}
So, before loading local vendor/autoload.php
, it detects that there is no such a file. It doesn’t matter because the Bedrock’s vendor autoloader is in use (looking into web/wp-config.php
we can see require_once(dirname(__DIR__) . '/vendor/autoload.php');
) and our custom plugin will be able to autoload your classes in its src
folder just fine.
I also recommend putting your custom plugin on a GitHub repo (or Bitbucket) and adding it to the Packagist.org. If the plugin is too private, just use the custom repo composer’s functionality as you’ve done.