I’m not aware of a function to check if a plugin has loaded.

It’s possible to do this:

if( kirby()->plugin('plugin-name') ) {
  echo 'Plugin has loaded';

…but it does not just check if the plugin has loaded. It also force load the plugin. That might not be what we want in all cases.

It’s also possible to do this:

if( file_exists(kirby()->root()->plugins() . DS . 'plugin-name' ) ) {
  echo 'Plugin has loaded';

…but that require that it needs to read from the folder to see if it exist or not.


Because a plugin already has been loaded, Kirby should already be aware of this. Therefor I suggest a function like this:

if( kirby()->pluginLoaded('plugin-name') ) {
  echo 'Plugin has loaded';
1 Like

What is your use-case for this? Do you have a code example where you would use this method?

Not a perfect one, but I have one case that is close to an example:

You see that if( kirby()->plugin('modules') )?

Yes, I have support for your plugin in one of my plugins now. :slight_smile:

Anyway, in this example I check if the Modules plugin exists in order to see if I should allow registry on that type. If the plugin is not active, it should skip this step.

Why it’s not a perfect example

I know that if the Modules plugin is active if it exists because I force load all the plugins before mine. That’s why it’s not a perfect example.

Why the suggested feature

I feel like it’s two functions in one. If I just want to check if a plugin has loaded, I also need to load that plugin. It’s like atoms, I can’t measure them without changing them (Observer_effect).

But if you don’t see any real case of the need for breaking out the conditional function, I guess there are no need for it. Maybe I should get back to this when I have a better example of a real case?

But what you need is not a “plugin was loaded” check but a “plugin exists” check. The latter is more useful in my opinion. :slight_smile:

Following @lukasbestle suggestion you could check if a plugin exists with simple functions:

function pluginExists($name) {
    return file_exists(kirby()->roots->plugins() . DS . $name);

Since some plugins also register a function, you could actually check if it has been loaded:

function pluginIsLoaded($name) {
    // or using class_exists
    return function_exists($name);

Both ways would work with the Modules plugin, by the way:

pluginExists('modules'); // true or false

pluginIsLoaded('modules'); // true or false

Yes, that’s correct. But please note that your pluginIsLoaded() function isn’t 100% reliable. I’d use the $kirby->plugins array instead.


Cool! Didn’t about that :arrow_up:

1 Like

And NOW you tell us this? :stuck_out_tongue:

Then we already have the solution right here:

if( a::get( kirby()->plugins, 'modules' ) ) {
	echo 'The Modules plugin has been loaded';

Well, it is also not an official method, it’s just reading out the internal property that is coincidentally a public property. :wink:

That’s because it’s not a public API; it’s an internal method with its own idiosyncrasies.
Kirby does not use “private” much or at all in classes. Also the code style favors short nouns rather than long method names. So one would be forgiven for thinking that a method is a generic and public tool, when actually it’s an internal and rather specific tool. So I consider that if it’s not detailed in the docs, it’s a private API. ^^

Won’t work if you have plugins installed with Composer though.

Also it might not work depending on loading order. That’s how the plugin’s main PHP script is included and the $kirby->plugins array is populated:

if(file_exists($file)) return $this->plugins[$name] = include_once($file);

So if your plugin is named hello, it will probably be loaded before modules, and the $kirby->plugins array will have no modules key.

If you wait for “later”, e.g. with code in a function that will be called in a controller or template, then it gets a bit more reliable.

@jenstornell, what are you trying to do exactly? Maybe there’s another way. :slight_smile:

1 Like

Pretty much, yes.

That’s true. But code that is loaded with Composer is not a Kirby “plugin”. Kirby will never be able to provide an API for them.

That’s correct, but it’s the intended behavior of such a “plugin is loaded” method. If you’d like to know if the plugin will get loaded, you’d need the “plugin exists” method.

It already does: the Registry API. :slight_smile:

For instance, what you do in:

could be done with Composer as well.

In the Twig plugin (v3), I only define classes and offer a static method (Kirby\Twig\Plugin::register) that uses the Registry and includes the plugin’s helper functions (that last part could be done by telling Composer to require a script, in addition to autoloading; but I like having it all in one place). Usage is fairly straightforward.

Well, but that’s something the plugin needs to support. It won’t work to include plugins in the plugins array automatically without some manual action by the plugin and/or the user.