Skip to content
  1. Blog

Prefix Namespaces in WordPress Plugins to Avoid Conflicts

Ben Word Ben Word on

If you’re bundling Composer dependencies in your WordPress plugin, you need to namespace them. You’re setting up your users for conflicts with other plugins if you don’t implement proper namespacing.

The problem

WordPress has no centralized dependency management. When multiple plugins bundle the same library at different versions, PHP loads whichever one gets registered first. This creates unpredictable race conditions that break plugins.

The psr/log library is a common offender. Several major plugins ship different versions, causing conflicts with other WordPress plugins and even Composer packages like Acorn.

Doing it wrong

The Acorn compatibility documentation calls out several plugins that are known to bundle conflicting dependencies without proper namespacing:

  • WooCommerce PayPal Payments doesn’t prefix psr/log, resulting in conflicts
  • WooCommerce UPS Shipping doesn’t prefix psr/log, resulting in conflicts
  • WooCommerce USPS Shipping doesn’t prefix psr/log, resulting in conflicts

These conflicts force WordPress developers to create workarounds and patches when they should be fixing the root cause: unnamespaced dependencies.

The solution

Use PHP-Scoper to prefix all your Composer dependencies with a unique namespace. This isolates your code completely from other plugins.

How Yoast does it right

Yoast SEO is an good example of proper dependency management. They use PHP-Scoper to namespace all their dependencies, including psr/log, preventing the exact conflicts that exist in the WooCommerce plugins mentioned above.

Screenshot of Yoast SEO with prefixed namespaces in their dependencies

In their repository, you can see their organized PHP-Scoper configuration in config/php-scoper, with separate config files for different dependencies.

This ensures their prefixed YoastSEO_Vendor\Psr\Log namespace never conflicts with other plugins, even those shipping outdated versions of psr/log.

Your responsibility

Plugin developers need to wrap their dependencies with their own namespace to prevent conflicts. It’s not enough to rely on other plugin authors to do the right thing.

Tools like PHP-Scoper exist to solve this problem. Use them.

Resources

Discuss this post on Roots Discourse

About the author

Ben Word

Ben Word has been creating WordPress sites since 2004. He loves dogs, climbing, and yoga, and is passionate about helping people build awesome things on the web.

Subscribe for updates

Join over 8,000 subscribers on our newsletter to get the latest Roots updates and tips on building better WordPress sites

Looking for WordPress plugin recommendations, the newest modern WordPress projects, and general web development tips and articles?

One last step! Check your email for a verification link.