Creates a custom autoloader for a plugin or theme.

Installs: 86 334

Dependents: 7

Suggesters: 0

Security: 0

Stars: 9

Watchers: 3

Forks: 2


v2.0.2 2020-07-09 13:18 UTC


This is a custom autoloader generator that uses a classmap to always load the latest version of a class.

The problem this autoloader is trying to solve is conflicts that arise when two or more plugins use the same package, but one of the plugins uses an older version of said package.

This is solved by keeping an in memory map of all the different classes that can be loaded, and updating the map with the path to the latest version of the package for the autoloader to find when we instantiate the class. This only works if we instantiate the class after all the plugins have loaded. That is why the class produces an error if the plugin calls a class but has not loaded all the plugins yet.

It diverges from the default Composer autoloader setup in the following ways:

  • It creates an autoload_classmap_package.php file in the vendor/composer directory.
  • This file includes the version numbers from each package that is used.
  • The autoloader will only load the latest version of the library no matter what plugin loads the library.
  • Only call the library classes after all the plugins have loaded and the plugins_loaded action has fired.


In your project's composer.json, add the following lines:

    "require-dev": {
        "automattic/jetpack-autoloader": "^1"

After the next update/install, you will have a vendor/autoload_packages.php file. Load the file in your plugin via main plugin file.

In the main plugin you will also need to include the files like this.

require_once . plugin_dir_path( __FILE__ ) . '/vendor/autoload_packages.php';

Current Limitations

We currently only support packages that autoload via psr-4 definition in their package.

Your project must use the default composer vendor directory, vendor.