Support » Developing with WordPress » Plugin importing other plugins code inside

  • eduardcot

    (@eduardcot)


    Developing a plugin I’ve encoutered a problem on using inside other plugins as a library.

    1. I create a plugin with a vendor folder where I put the code of another plugin that I want to reuse. For example a Mobile_User_Agent_Detector.

    2. In my plugin I import a class from Mobile_User_Agent_Detector using require_once. Then I use the class contained in that php file.

    For example:

    require_one ..../my_plugin/vendor/mobile-user-agent-detector.php
    
    $detector = new Mobile_User_Agent_Detector()

    3. In a wordpress I install my plugin and another plugin that it’s using Mobile_User_Agent_Detector as well.

    4. It fails. Because when I import the Mobile_User_Agent_Detector it’s already defined.

    I’ve seen plugins that wraps any global method or class definitions from the THIRD_PARTY_PLUGIN to check if it’s already defined. Is there a better way to do import another library and not have collissions with other plugins?

    How are you approaching this problem?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    All publicly available plugins should be visible to WP and not hidden in your plugin folder for that very reason. If you must do so for some reason, you need to mask it’s references through unique classes or namespaces, so in essence that hidden plugin becomes a fork that you’re responsible for.

    For publicly available plugins, it’s better to leave them as their own entity and check if they are active through the “active_plugins” option before doing anything dependent upon that plugin. Then other devs that want to depend on that plugin can use the same resource instead of creating another fork of the same thing.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.