i want to ask a simple question that what to do if i have to check whether a pliugin is installed. for example i want to run a code, which says,

if Gutenberg is installed, then run the following code else not.

i have created this code, but not sure will this work or not

    if(defined('Gutenberg')) {
    }

is this the right way or is there any other way to that. thanks in advance

  • Mark's answer below is good advice. but if you specify in your question what exactly you want to do if Gutenberg is or isn't present then it would be possible to provide an answer specific to what you're trying to do. – Jacob Peattie Aug 10 at 9:08
up vote 4 down vote accepted

Checking if a plugin is installed is always a bad idea as plugins might be installed but for whatever reason not function, or function in different way than you might expect.

Specifically for gutenberg as it stands right now for example, post types that are not exposed in the REST API can not be edited by gutenberg.

As always, if you have a functionality that depends on a plugin you should hook to its actions and filters to avoid the need to guess if it is active or not (the fact that the action is "called" is the best indication for activity), or ask the plugin author to add the kind of action/filter you might find useful.

One possible way to do this is to check whether a specific class or function that is included in the plugin is defined, with class_exists() or function_exists(), respectively.

For Gutenberg, you could check for register_block_type():

if ( function_exists( 'register_block_type' ) ) {
    // Gutenberg is available.
}

I would however side with Mark Kaplun to say this is not a good idea in most cases, and it is not even necessary if you follow best practices.

If you are going to interact with a plugin, you should do so via hooks and filters. Whether the plugin is running or not is irrelevant: if the plugin isn't there the hooks simply won't run.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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