1

We have developed 2 WordPress plugins which are using same composer package. Depending on plugin versions the package included in plugin may be changed, we constantly add new functionality to our package. The problem is that for example plugin A have version 1.0.0 of package, and plugin B have version 1.0.1, WordPress loads only one package , from plugin which loaded first, so if plugin A loaded first then plugin B will use version 1.0.0 package. We are including autoload.php on both plugins.

Is it possible to do some configuration in WordPress or from composer side to make every plugin load and work with package included on his vendor folder ?

5
  • 1
    Does this answer your question? Installing multiple versions of a composer package Commented Feb 10, 2023 at 13:10
  • 4
    A common method in WordPress is to use a tool such as Namespacer, PHP-Scoper or Imposter to effectively assign a new root namespace to each of those libraries on a per-plugin basis. It is ugly and hacky but works. Commented Feb 10, 2023 at 14:18
  • yup i already find PHP-Scoper , and yes its ugly :/ Commented Feb 10, 2023 at 17:44
  • Ultimately this isn't a WordPress or composer problem, it is just a fact of PHP. During the lifetime of a PHP request, once a class is declared it can't be unset or redeclared, and there's no way to partition things, short if creating a new (sub) request. I guess both plugins could declare a global version const, and then your autoloaders could inspect both and only the "newest" loads. Or maybe instead of a const you could throw a filter in there. Or, switch to composer-based WordPress and manage it at a higher level. Commented Feb 10, 2023 at 18:46
  • This question is similar to: 3rd party dependency conflict in developing Wordpress Plugin. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Nov 29, 2024 at 8:35

1 Answer 1

1

Is it possible to do some configuration in WordPress or from composer side to make every plugin load and work with package included on his vendor folder ?

In general no. The option about namespace rewriting has already been given by Chris Haas, so if you need to rely on different code behind the same global static names, you have to provide a different name for each version.

Another option is you align both plugins to rely on the same dependency version-stability. The example you give with 1.0.0 and 1.0.1 versions, the API should be compatible and it should not be an issue (if the package follows semantic versioning).

From the context of your question, it seems the dependency has not yet matured enough that this is an option practically.

Instead it should be possible to have the plugin which is currently based on 1.0.0 to use 1.0.1 as well. Then the version conflict is solved, as now it is the same version and can use the same names. This may not be an ideal solution, but could get you back to a working version fast and gives you more room to consider a better way in the future (e.g. having a build for those plugins that use scoping).

Additionally you could/should wrap all access to third-party libraries once in your own code so that such problems aren't that deep reaching (dependency issues tend to be harder to resolve). This methodology is independent to Wordpress or Composer, just a recommendation on how to interface to third-party dependencies (or even Wordpress itself[1], which you may know better).

If however what you ask here is already such a wrapper you build your own (e.g. in form of a composer package), consider to adhere to semantic versioning and stabilize its API first.

PHP has no built-in utility to create archives apart from PHAR, e.g. that you can have imports via names but to different code. This is likely also the reason why Composer does not support it and instead provides PSR-0/PSR-4, classmap and file inclusion autoload configuration.

As you have the scenario to share the same namespaces across the plugins - there is no other option actually as it runs in the same PHP process - the first one wins. You may however make your own plugins interoperable so that they can establish a loading order in their own hierarchy, e.g. to prefix the one autoloader before the other conditionally if it exists.

I'd probably go with scoping in the first place and only share build utilities between the plugins nowadays and have a nice package for each plugin afterwards. Yes, scoping can be PITA, but if you have this early on, you don't have to solve this later which is much harder.


  1. Compare for the level of WordPress plugins WordPress Plugin: How do I avoid "tight coupling"? and Multiple Custom Metabox Help - both well dated so it is easier to not take code verbatim but to develop own conclusions.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.