automattic/jetpack-autoloader
Composer autoloader drop-in for WordPress and PHP projects that avoids class conflicts across bundled plugins/themes. Provides a custom autoloader to isolate vendor libraries, improve compatibility, and reduce “cannot redeclare class” errors in complex installs.
Install the Package
Add to your plugin/theme's composer.json:
{
"require": {
"automattic/jetpack-autoloader": "^2"
}
}
Run composer install or composer update.
Load the Autoloader
In your plugin's main file (e.g., my-plugin.php), include the generated autoloader:
require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload_packages.php';
Verify Compatibility Ensure all plugins/themes using shared dependencies also adopt this autoloader to guarantee version consistency.
If your plugin relies on a package (e.g., vendor/package) and another plugin uses an older version, this autoloader ensures the latest version is loaded. Example:
// Instead of relying on Composer's default autoloader,
// this will always load the newest version of `Vendor\Package\Class`.
new Vendor\Package\Class();
Autoload Generation
composer dump-autoload -o to generate optimized classmaps (jetpack_autoload_classmap.php).JETPACK_AUTOLOAD_DEV for dev packages (e.g., 9999999-dev).Plugin Integration
autoload_packages.php early (before plugins_loaded if possible).plugins_loaded to defer non-critical autoloads and avoid conflicts:
add_action('plugins_loaded', function() {
// Safe to load classes here.
new Vendor\Package\Class();
});
Development vs. Production
define('JETPACK_AUTOLOAD_DEV', true); to prefer dev-* versions.2.0.0 > 1.0.0).PSR-4 to Classmap Conversion
The autoloader converts PSR-4 autoloads to classmaps during composer dump-autoload -o, improving performance.
Transient Caching Uses WordPress transients to cache plugin lists, reducing lookup overhead. Clear transients with:
delete_transient('jetpack_autoloader_plugins');
Debugging Early Loads
Enable debug mode to catch classes loaded before plugins_loaded:
define('JETPACK_AUTOLOAD_DEBUG_EARLY_LOADS', true);
--optimize with Composer commands for faster loads in production.Classmap Staleness
composer dump-autoload -o
Version Hierarchy Conflicts
0.1.0) may be ignored if an older package (e.g., 2.0.1) exists.Transient Cache Masking
define('JETPACK_AUTOLOAD_DEBUG_CONFLICTING_LOADERS', true);
Early Loading Errors
plugins_loaded may conflict with later plugins.JETPACK_AUTOLOAD_DEBUG_EARLY_LOADS and defer loads:
add_action('plugins_loaded', function() { /* Load here */ });
Composer Autoloader Conflicts
autoload.php) may override this one.spl_autoload_register() priorities.var_dump(file_exists(plugin_dir_path(__FILE__) . 'vendor/autoload_packages.php'));
vendor/composer/jetpack_autoload_classmap.php for missing/incorrect entries.error_log:
define('JETPACK_AUTOLOAD_DEBUG', true);
AutoloadGenerator to modify classmap logic (e.g., exclude files).JETPACK_AUTOLOAD_DEV to force dev versions during testing.jetpack_autoloader_classmap filter:
add_filter('jetpack_autoloader_classmap', function($classmap) {
$classmap['Vendor\\CustomClass'] = __DIR__ . '/custom-path.php';
return $classmap;
});
vendor/ directory (not custom paths).declare(strict_types=1).-o flag for ~30% faster loads (classmaps instead of PSR-4).add_filter('jetpack_autoloader_transient_lifetime', function() {
return HOUR_IN_SECONDS; // Shorter cache for testing.
});
How can I help you explore Laravel packages today?