WordPress and Composer · vanrossum.dev    

       vanrossum.dev 

 Article

WordPress and Composer
======================

 ![](https://vanrossum.dev/images/jeffrey-portrait.webp) By Jeffrey van Rossum

  vanrossum.dev  

  [      vanrossum.dev ](https://vanrossum.dev)                      

  [ ← Writing ](https://vanrossum.dev/posts)   [ ← Writing ](https://vanrossum.dev/posts) [ The challenges ](#the-challenges) [ Potential Solutions ](#potential-solutions) [ Jetpack Autoloader ](#jetpack-autoloader) [ My Preferred Choice ](#my-preferred-choice) [ Sources ](#sources) 

Using Composer to manage and load packages is commonplace in PHP projects, and most of the time, it just works. However, if you're developing a WordPress plugin or theme, you might encounter some challenges.

![WordPress and Composer](https://i.ibb.co/16gbDVY/Screenshot-2023-08-22-at-13-38-53.png)

The challenges
--------------

WordPress was created in a pre-dependency-management era. Its architecture, compared to a framework like Laravel, isn't as well-suited for a dependency manager like [Composer](https://getcomposer.org).

You might wonder, "Why can't I simply use Composer within the context of a WordPress plugin?" That's an excellent question.

A typical WordPress site features multiple plugins. If another plugin leverages the same package, you can hope they'd both use the same version. But, as [Iain Poulsen](https://deliciousbrains.com/dependency-management-wordpress-proposal/) nicely remarked, "It's like playing the lottery."

You very well might run into compatibility issues.

Potential Solutions
-------------------

The open-source community offers not one but several remedies to this dilemma. Each has its own strengths and weaknesses.

### Humbug’s PHP Scoper

[PHP Scoper](https://github.com/humbug/php-scoper) wasn't explicitly made for WordPress plugins, yet it's configurable for this need. PHP Scoper takes all project dependencies, and ensures their uniqueness by prefixing every class and method. The popular SEO plugin, Yoast, [uses it](https://github.com/Yoast/wordpress-seo/blob/trunk/composer.json#L120).

Once, I experimented with PHP Scoper. Check out the results [on GitHub](https://github.com/jeffreyvr/wp-plugin-php-scoper-test/tree/c50eb3070837f9bbb58712c812e7a0aff527966e).

### Mozart

[Mozart](https://github.com/coenjacobs/mozart) is specifically developed for WordPress plugins. PHP Scoper is a more generic solution for PHP projects and also supports FCQN prefixes, while this is not the case with Mozart. Mozart also mentions PHP Scoper as a good alternative and notes that when Mozart was being developed, this tool was not yet available. However, Mozart does support classmap autoloaders, unlike PHP Scoper. Just like PHP Scoper, Mozart adds prefixes to all classes and methods.

### Imposter

[Imposter](https://github.com/TypistTech/imposter) shares similarities with Mozart. A notable limitation of Imposter is its inability to support traits and virtual packages.

Jetpack Autoloader
------------------

[Jetpack Autoloader](https://github.com/Automattic/jetpack-autoloader) produces its own autoloader, ensuring the most recent class version is consistently loaded. Differing from a prefix-based solution, Jetpack Autoloader offers an alternative method.

A downside, is that it works when other WordPress plugins use it too. But popular plugins, such as Jetpack and WooCommerce - both active on millions of websites - use it.

Still, while using the latest class version might diminish compatibility concerns, it doesn't entirely eradicate them.

[Vimeo Video Links](https://github.com/jeffreyvr/vimeo-video-links/tree/main) is an open-source plugin I made, using Jetpack Autoloader.

My Preferred Choice
-------------------

Now, having explored four potential solutions, I'll share my preference with you. I can sense your anticipation! It's Jetpack Autoloader.

Though prefixing is probably the safest path, it often entails:

- A complex configuration/setup process
- Occasionally unclear documentation
- Resulting in a plugin's built version or a distinct vendor folder
- Occasionally lacking features, like Imposter's inability to support traits

Yet, once set up correctly, compatibility issues are typically non-existent. But you might run into other gotchas.

Although Jetpack Autoloader doesn't wholly resolve the compatibility concern, its setup simplicity is unparalleled. While other tools might merit their separate how-to-posts, setting up Jetpack Autoloader is a cinch. Simply execute `composer require automattic/jetpack-autoloader` and load `autoload_packages.php` instead of `autoload.php`.

If it's the chosen tool for top-tier plugins, such as Jetpack and WooCommerce, it certainly meets my standards.

Sources
-------

During my research on this, I encountered several helpful articles. I've listed them below:

- [Avoid Composer Dependency Hell In WordPress Plugin Development](https://richaber.com/blog/2019/07/01/avoid-composer-dependency-hell-in-wordpress-plugin-development/)
- [Dependency Management and WordPress: A Proposal](https://deliciousbrains.com/dependency-management-wordpress-proposal/)
- [How to Avoid Namespace Issues in your Composer Dependencies](https://deliciousbrains.com/php-scoper-namespace-composer-depencies/)
- [Package Management in WordPress: Introduction and Solutions](https://inpsyde.com/en/package-management-in-wordpress-introduction-solutions/)
- [Using PHP dependencies in WordPress](https://developer.yoast.com/blog/safely-using-php-dependencies-in-the-wordpress-ecosystem/)

  ![](https://vanrossum.dev/images/jeffrey-portrait.webp) Jeffrey van Rossum [@jeffreyrossum](https://x.com/jeffreyrossum) 

 share [𝕏](https://twitter.com/intent/tweet?url=https%3A%2F%2Fvanrossum.dev%2Fposts%2Fwordpress-and-composer&text=WordPress+and+Composer) [WhatsApp](https://wa.me/?text=WordPress+and+Composer+https%3A%2F%2Fvanrossum.dev%2Fposts%2Fwordpress-and-composer) [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fvanrossum.dev%2Fposts%2Fwordpress-and-composer) 

   © 2026 Jeffrey van Rossum 

  vanrossum.dev vanrossum.dev
