Skip to content

Instantly share code, notes, and snippets.

@mbrughi
Created November 7, 2022 13:42
Show Gist options
  • Save mbrughi/b45e940032217407151bdc026acd7dd8 to your computer and use it in GitHub Desktop.
Save mbrughi/b45e940032217407151bdc026acd7dd8 to your computer and use it in GitHub Desktop.
Wordpress: Check if plugin is active (various examples) (NB: do not copy the entire code but only the necessary snippet )
<?php
/**
* Detect if a WordPress plugin is active
* (various examples)
*/
/* 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 folder. Check if WooCommerce is active //docs.woocommerce.com/document/create-a-plugin/
// Test to see if WooCommerce is active (including network activated).
$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . 'woocommerce/woocommerce.php';
if (
in_array( $plugin_path, wp_get_active_and_valid_plugins() )
|| in_array( $plugin_path, wp_get_active_network_plugins() )
) {
// Custom code here. WooCommerce is active, however it has not
// necessarily initialized (when that is important, consider
// using the `woocommerce_init` action).
}
// or simple
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 (use only on front-end)
# Example. Check if QueryLoop Visual Attributes is active
if ( is_plugin_active( 'ql-visual-attributes/ql-visual-attributes.php' ) ) {
// Visual Attributes is activated
}
// Backend example
# 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