0

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;
}
1
  • Normally you would have a project level composer.json so that there's a singular vendor folder in wp-content that's loaded which contains the shared dependencies. Then the WP custom installer would make sure the themes and plugins themselves went to the right folders rather than the vendor folder. The situation you're in shouldn't occur, can you give us some more context as to the structure?
    – Tom J Nowell
    Oct 12, 2020 at 11:59

1 Answer 1

1

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/

1
  • 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, 2017 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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