Skip to content

Instantly share code, notes, and snippets.

@llgruff
Last active October 25, 2022 09:29
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save llgruff/c5666bfeded5de69b1aa424aa80cc14f to your computer and use it in GitHub Desktop.
Save llgruff/c5666bfeded5de69b1aa424aa80cc14f to your computer and use it in GitHub Desktop.
A function you can use to check if plugin is active/loaded for your plugins/themes
<?php
/**
* Detect if a WordPress plugin is active
* A function you can use to check if plugin is active/loaded for your plugins/themes
* @link //gist.github.com/llgruff/c5666bfeded5de69b1aa424aa80cc14f
*/
// When coding plugins that rely on another one, like Private Content for bbPress or Visual Attributes for WooCommerce, you need to make if the WordPress Plugin is active to initialize your plugin routines or display a notice saying that the required plugin must be activated. In this tutorial we’ll see how to detect whether a certain plugin is active in a couple of ways.
## 1. Check whether a certain class or function or constant exists
// While these methods can be used to know if a plugin is activated, they’re also useful for plugins with a large amount of modules like Jetpack or WPML so we can figure out whether a particular module is enabled.
# Example. Check if bbPress plugin is loaded
if ( class_exists( 'bbPress' ) ) {
// bbPress is enabled so let's begin
}
# Example. Check if Jetpack's Sharing module is available
if ( function_exists( 'sharing_display' ) ) {
// sharing is enabled, do something
}
# Example. Check if CMB2 is loaded
if ( defined( 'CMB2_LOADED' ) ) {
// code that requires CMB2
}
# Example. Check if WooCommerce is activated
if ( class_exists( 'WooCommerce' ) ) {
// code that requires WooCommerce
}
# Example by plugin author. Check if WooCommerce is active //docs.woocommerce.com/document/create-a-plugin/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
// WooCommerce is active
}
## 2. Using the standard function is_plugin_active
// This function is initially only available in the WP Admin, and as such, code using it must be hooked to admin_init or a later action. It’s possible to use it in the front end but you need to load the file wp-admin/includes/plugin.php with the additional load it implies. Its usage is like
# Example. Check if QueryLoop Visual Attributes is active
if ( is_plugin_active( 'ql-visual-attributes/ql-visual-attributes.php' ) ) {
// Visual Attributes is activated
}
# Example. Check if QueryLoop Visual Attributes is active [to use in front end]
include_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( is_plugin_active( 'ql-visual-attributes/ql-visual-attributes.php' ) ) {
// Visual Attributes is activated
}
## 3. Check if the plugin is active with custom function
// While the two first approaches above are good (third one is also good but we have to load that additional file), there might be a case when the required plugin only exposes its classes and functions after a certain moment and they’re are not yet available when your own plugin is first activated. In these cases we can check if the plugin is activated in single site or network wide.
/**
* Checks if the required plugin is active in network or single site.
* @param $plugin
* @return bool
*/
function main_is_plugin_active( $plugin ) {
$network_active = false;
if ( is_multisite() ) {
$plugins = get_site_option( 'active_sitewide_plugins' );
if ( isset( $plugins[$plugin] ) ) {
$network_active = true;
}
}
return in_array( $plugin, get_option( 'active_plugins' ) ) || $network_active;
}
## 4. Combining the methods
// To get the best of both methods, class/function detection and active plugin detection, you can use:
# Example. Check if WooCommerce is active
if ( main_is_plugin_active( 'woocommerce/woocommerce.php' ) || class_exists( 'WooCommerce' ) ) {
// it's active so let's initialize our plugin.
} else {
// it's not active. Notify the user, perhaps displaying a notice.
}
# Example. Check if CMB2 is loaded
if ( main_is_plugin_active( 'cmb2/init.php' ) || defined( 'CMB2_LOADED' ) ) {
// it's active so let's initialize our file
require_once get_template_directory() . '/functions/cmb2.php';
} elseif ( is_admin() && current_user_can('manage_options') ) {
// it's not active. Notify the user, perhaps displaying a notice.
add_action( 'admin_notices', function(){
echo '<div id="message" class="error notice is-dismissible"><p>CMB2 plugin <b>is not active!</b></p></div>';
} );
}
// With these methods we are now sure if your plugin is active or not and we can safely run ours.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment