• Resolved Imageislp

    (@islp)


    Hello, I have a somewhat unusual question: I am a bit confused and there are some things I don’t understand about how plugins work as a whole.

    I would like to create a Utils plugin that will be used by my other custom plugins (these are not public plugins, they are only used by a specific website).

    So, the idea is that, for example, this plugin will contain a class with a series of static methods that can be called by other plugins.

    But one thing is not clear to me: how can I be sure that the “Core” plugin is available and loaded when I call one of its methods from another plugin?

    Could a plugin be loaded before this “Core” plugin?

    Are all plugins loaded before they can be used?

    Does the “Requires-Plugin:” entry cause a plugin to be loaded before another plugin on which it depends?

    Any help is appreciated

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Imagebcworkz

    (@bcworkz)

    Plugins are loaded fairly early, so if a plugin is active, its code will be available once WP has fully loaded, such as during or after the “init” action. You can get an array of active plugin file paths (relative to /plugins/ directory) via the “active_plugins” option.

    It appears that plugins load in alphabetical order, but it’s not documented. Best to assume load order is undefined, other than must-use plugins load first, then multi-site if applicable, then finally regular plugins. If your plugin code executes as action or filter callbacks, as would normally be the case, all active plugin code will be available for use.

    AFAIK “Requires-Plugin:” is simply for information, it has no bearing on what actually happens in the load process.

    Thread Starter Imageislp

    (@islp)

    @bcworkz ok, thanks. In my specific case, method calls come way after the plugins are loaded, so it could be safe to use any class. Another option would be the mu-plugins directory but I red sometimes it gets attacked.

    I don’t know who removed the AI generated answer but it was a good thing to do it.

    ImageAlessandro Lin

    (@alessandro12)

    Two examples of checking whether a plugin is active:

    // check if woocommerce is active. Syntax
    if(!empty(is_plugin_active("woocommerce/woocommerce.php"))){
    // code
    }


    // check if cf7 is active. Syntax
    if(!empty(is_plugin_active("contact-form-7/wp-contact-form-7.php"))){
    // code
    }

    Thread Starter Imageislp

    (@islp)

    @alessandro12 That’s absolutely fine, but at that point you need a clear strategy: what happens if the plugin isn’t active? I feel that this would end up overcomplicating something that should be much simpler (at least, in my scenario). Anyway, even if I could create a sort of “master plugin”, I ended up moving my static methods to a custom mu-plugins directory

    If your plugin needs to rely on other plugins being installed, there’s two things that you need to do.

    First, make sure all of your function calls are added into the ‘plugins_loaded’ action so that you know that everything is loaded before you try to use it.

    Second, you should set up your plugin to require the other plugins before it can be activated. Read more about this at Plugin Dependencies. That way WordPress will make sure that the other plugins are installed and active before it allows your plugin to be activated, and it won’t allow the other plugins to be deactivated without your plugin being deactivated.

    Of course, you should also do error checking in your code to make sure that the external functions/classes are available before you do anything with them just in case something has gone wrong as a required plugin has been manually removed.

    Thread Starter Imageislp

    (@islp)

    @catacaustic

    No: any custom plugin needs this plugin that contains common functionalities.

    As explained by another user, requiring (plugin dependencies) is not effective.

    I solved by using the mu-plugins directory that loads before the plugins are loaded: there I put traits and static methods any plugin in any namespace can easily and safely use.

    Yes, a plugin can load before another one depending on the load order. A common solution is to check if the core plugin class or function exists before using it, or load your code on hooks like plugins_loaded to ensure all plugins are available. If your plugins depend on a core utility plugin, you can also add a dependency check (like class_exists) and show an admin notice if the core plugin is not active. This helps avoid errors and keeps the plugins working safely.

    Thread Starter Imageislp

    (@islp)

    @ryanmax22 checking for the existence of a method or class is partially useful: as I told before, if you do that, then you need to cover the case when the class is not available (this means other code and complexity). Anyway, in my particular scenario (so NOT universally speaking) the mu-plugins idea is just perfect 🙂

Viewing 8 replies - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.