If you are building a WordPress plugin that depends on another plugin, you will want to check if the required plugin is installed and activated or not. You can do so very easily. This is very useful for plugins that are add-ons for other plugins.

The easiest way to check if a plugin is active is to get a list of all active plugins and then check if the required plugin is in that list. Here’s how you do it:

$plugins = get_option('active_plugins'); // Get a list of all active plugins.

// The main PHP file that of the required plugin.
// A plugin's main file is the same name as the name of the plugin's folder.
$required_plugin = 'required_plugin_name/required_plugin_name.php';

if ( in_array( $required_plugin , $plugins ) ) {
	$required_plugin_active = true;
}else{
	$required_plugin_active = false;
}

In the above code, we first get a list of all the active plugins on our site using the get_option function. It returns an array of all active plugins. The variable $required_plugin is a string that contains the location of the main file of the required plugin. The main file of a plugin is the file that has the same name as the plugin’s folder name. So, if the folder name of a plugin is ‘my_custom_plugin’, then the name of the main file that WordPress loads will be ‘my_custom_plugin.php’.

The code then checks if the required plugin’s directory location is in the array of active plugins. if it is, then it means our required plugin is active. The variable $required_plugin_active then is set accordingly.

Pin It on Pinterest

Share This