Detecting if a WordPress plugin is loaded has several benefits.
Prevent site from crashing, either to "White Screen Of Death, WSOD" or in worst case displaying an error message to the visitor.
Increase performance by not running code when a WordPress plugin is disabled or not installed.
Progressive enhancement, enable more advanced features that leverage other installed plugins when they are available, provide basic functionality when they're not available.
Display message to enable required plugins in WordPress backend (WP Admin).
There are probably many more great use cases, but these are the ones I've found useful over the years.
WordPress keeps a record in the database of which plugins are activated, retrieve it directly using get_option('active_plugins').
It returns an array of file-paths to the main php file of each active plugin, relative to (excluding) the WordPress plugin directory, for example
woocommerce/woocommerce.php for WooCommerce.
The simplest implementation:
if (in_array('plugin-dir/plugin-file.php', get_option('active_plugins', []))) {
// TODO: add code dependent on particular plugin here
}
Wrapped in a function for easy reuse:
function YOUR_OWN_PREFIX_is_plugin_active($plugin_path) {
return in_array($plugin_path, get_option('active_plugins', []), true);
}
if (YOUR_OWN_PREFIX_is_plugin_active('plugin-dir/plugin-file.php')) {
// TODO: add code dependent on particular plugin here
}
Lets say this code is in your custom theme in the function.php file and the WooCommerce plugin is not installed or has been disabled for some reason.
When you load a page containing the code the page goes blank or displays a PHP error message.
if (YOUR_OWN_PREFIX_is_plugin_active('woocommerce/woocommerce.php')) {
function my_hook_handler($a, $b) {
...
}
add_action('woocommerce_hook', 'my_handler', 10, 2);
function my_filter($value) {
... // modify value or do stuff based on value
return $value;
}
add_filter('woocommerce_filter', 'my_filter', 10, 1);
...
}
Only adding code when it can actually be used can improve performance and uses less memory.
if (YOUR_OWN_PREFIX_is_plugin_active('woocommerce/woocommerce.php')) {
function my_hook_handler($a, $b) {
...
}
add_action('woocommerce_hook', 'my_handler', 10, 2);
function my_filter($value) {
... // modify value or do stuff based on value
return $value;
}
add_filter('woocommerce_filter', 'my_filter', 10, 1);
...
}
If a custom plugin or theme uses features from other plugins, display a notice to the WordPress administrator about enabling the required plugins.
if (!YOUR_OWN_PREFIX_is_plugin_active('required-plugin-name/required-plugin-name.php')) {
function my_admin_notice() {
$message = esc_html__('[My plugin name] requires [required plugin] to function. Please install [required plugin].', 'my_text_domain'),
echo sprintf('%s
', $message);
}
add_action('admin_notices', 'my_admin_notice');
}
COPYRIGHT © 2025 | TERMS & CONDITIONS | PRIVACY | BUILT IN SYSTEME.IO