Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top