WordPressPlugin101 icon indicating copy to clipboard operation
WordPressPlugin101 copied to clipboard

Two Plugin with same namespace

Open shadhin-msa opened this issue 6 years ago • 7 comments

Hi there, Thanks for your awesome youtube series.

The issue is, In your plugin, you used "Inc" namespace. What if another plugin uses same "Inc" namespace through composer?

I faced the issue. it loads files from one of the repositories src for both plugins. So one plugin gets broken.

A possible solution could be using a unique identifier for the namespace. But it's not the appropriate solution, is it? Any solution?

issue1

shadhin-msa avatar Jan 08 '19 10:01 shadhin-msa

Look at the lesson (WordPress Plugin Development - Part 10 - Namespaces and Composer Autoload ) https://www.youtube.com/watch?v=nbF4hWJ1hJA. It will explain everything.

bahiirwa avatar Jan 08 '19 11:01 bahiirwa

I went through the video and have gone through again. The video explains how to use the namespace through composer autoload and it's well explained. My plugin works well. Only gets broken when two custom plugin get installed (after activated) together. Because both are using "Inc" namespace. Check the screenshot I added.

shadhin-msa avatar Jan 08 '19 13:01 shadhin-msa

Exactly change the namespace of one in composer on the PSR-4 line and do a composer dump-autoload

bahiirwa avatar Jan 08 '19 13:01 bahiirwa

Ok then what if my plugin installed in a website where already a plugin using the same namespace.

shadhin-msa avatar Jan 08 '19 13:01 shadhin-msa

You are right. I deregistered all my services, changed initial file names in plugin folder, namespace and did a whole new composer install and something still duplicated.

bahiirwa avatar Jan 10 '19 15:01 bahiirwa

Shadhin...Thanks for your explanation on 1/8/219 of the exact problem I was having. Using the ideas above I changed the inc directory to my plugin name + Inc, changed globally (in the plugin) namespace Inc to namespace my plugin name + Inc, changed globally use Inc to use my plugin name + Inc, and in the main plugin PHP changed the activate, deactivate and Init (2 places) Inc to my plugin name + Inc. I then changed the psr-4 line in composer.json to read "psr-4": { " My plugin Name + Inc \" : "./ My plugin Name + Inc" } and ran composer dump-autoload. I now have three plugins operating independently with no collisions. Thanks to you all!!!

ldan4th avatar Feb 18 '20 21:02 ldan4th

Just found this issue too. Did a global Find/Replace with this regex in the new/modified plugin based on the Alecaddd model. From Inc(\\|;) To Inc_MYPLUGIN_SLUG$1 Then "composer dumpautoload". This is required, otherwise the $prefixLengthsPsr4 value in the autoload_static.php file has an incorrect count. When this is complete, multiple plugins can be activated and deactivated in any order, because they no longer share the global 'Inc' namespace.

Thanks for asking this question, @shadhin-msa .

The next question is, what do we do with critical information like this? Add it to the README.md? Add it to a wiki? If this valuable Issue is closed, others lose the benefit of the solution.

TonyGravagno avatar Jun 15 '21 01:06 TonyGravagno