I've got projects (both themes and plugins) set up as composer projects. These projects can be dev-dependencies of each other. Right now I'm working on a theme which requires the vendor/autoload.php file. However, when I bring that theme into another project (like a plugin), I still need access to the autoload.php. The problem is that the autoload.php file is now inside the directory structure for the plugin and not the theme.

define('FL_CHILD_THEME_DIR', get_stylesheet_directory());
define('FL_CHILD_THEME_URL', get_stylesheet_directory_uri());

if (!file_exists(FL_CHILD_THEME_DIR . '/vendor/autoload.php')) {
  // somehow check all the directories within the plugins directory 
  // for vendor/autoload.php
} else {
    require FL_CHILD_THEME_DIR . '/vendor/autoload.php';

I've tried using things like scandir and glob but I'm not getting any closer. I thought something like this would work, but I'm still no closer.

foreach(glob(plugin_dir_path() . 'vendor/autoload.php') as $file {
  require $file;

Autoloader setup:

"autoload"   : {
   "files"   : ["functions.php"]

Once you have the autoloader setup properly, you just need to include that one file, like this:

require_once( 'vendor/autoload.php' );

Full tutorial here - https://torquemag.io/2014/11/improving-wordpress-plugin-development-composer/

  • Thanks! This was a good read. It turned out not to quite fit my needs though. I was able to resolve the issue by mapping the vendor folder inside my docker-compose file. – aberkow Aug 1 '17 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.