12

As the title suggests, I was wondering how to check if a certain plugin exists (and has been installed).

And secondly, check that the plugin is actually enabled.

Thanks,

Mark

  • Can you elaborate? Do you need to check from somewhere else other than the "/admin/settings/plugins" screen? – Rick Jul 20 '15 at 23:14
16

On top of checking within the admin area, there are some other ways you can do this, both in templates and in your plugins.

In a template

{# Second argument is if you want enabled only #}
{% set plugin = craft.plugins.getPlugin('pluginHandle', false) %}

{# Now you have access to these methods... #}
{{ plugin.isEnabled() }}
{{ plugin.isInstalled() }}

For a full list of methods, see PluginVariable in craft docs.

For more info on how to get plugins within your templates, see the PluginsVariable, note this is different from PluginVariable.

In your plugin

To check a plugin from within your own plugin:

// Again, second arg is if you just want enabled plugins
$plugin = craft()->plugins->getPlugin('pluginHandle', false);

// Then you can do things like:
$plugin->isInstalled
$plugin->isEnabled;

See the PluginService and also the BasePlugin for more info.

Hope that helps :)

  • 1
    Thanks Alec, this is exactly what I was after. To elaborate on my purpose for this: My plugin relies on the basis that certain fieldtypes are created first. I could check for the fieldtype itself, but I found myself doing a lot of long winded (is defined and is not null and *fieldtype*|length) checks that was making the code look untidy. A simple check to see if the plugin is active in the first place allow me to create more informative conditions. Now I can tell the user "This page isn't working because..." as apposed to an immediate error page as soon as the plugin is installed. – Mark Notton Jul 22 '15 at 11:34
  • No worries! and if your plugin is relying on certain fieldtypes being created, are these something you could create within the onAfterInstall method on the plugin? that way you can always be sure the fieldtypes will be there and will also minimise user error, fieldtype handle mismatches and so forth. – Alec Ritson Jul 22 '15 at 13:52
  • Am I right in thinking that pluginHandle corresponds to the directory name of the plugin? Couldn't find any documentation on this, but after a bit of trial and error that's what worked for me. – Nick F Oct 12 '15 at 15:11
  • For craft cms 3 see me answer. – inrsaurabh May 15 at 7:42
4

A simple way to check that a plugin is both installed and enabled is to use Twig's is defined test (since craft.pluginHandle is defined only if the plugin is both installed and enabled):

{% if craft.pluginHandle is defined %}
  ...
{% endif %}

(Credit due to this answer)

4

For craft cms 3

{% set plugin = craft.app.plugins.getPlugin('plugin-handle', false) %}


{% if plugin is not null and plugin.isInstalled %}
....
    call plugin related stuff here
.....
{% endif %}

To check plugin handle.

  1. Open composer.json of your plugin.
  2. Search for handle param in that file, that's the plugin handle.
3

It should show up in the control panel in the plugins section. It will show the name of the plugin and if it is enabled. I've had issues when i didn't setup the folder correctly. Double check the plugin documentation, it should tell you what folder to place within the craft plugins directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.