In WordPress, you can check if a plugin is active using the is_plugin_active()
function. This function takes the plugin basename as its argument, which is the plugin’s folder name and main plugin file name, separated by a forward slash.
Here’s an example of how you can use is_plugin_active()
:
if ( is_plugin_active( 'my-plugin/my-plugin.php' ) ) {
// Do something if the plugin is active
} else {
// Do something if the plugin is not active
}
Alternatively, you can use the get_option() function to check if a plugin is active. The active_plugins option in WordPress stores an array of all active plugins. You can check if a plugin is active by checking if its basename is in this array:
$active_plugins = get_option( 'active_plugins' );
if ( in_array( 'my-plugin/my-plugin.php', $active_plugins ) ) {
// Do something if the plugin is active
} else {
// Do something if the plugin is not active
}
Both is_plugin_active() and get_option() are safe to use in any plugin or theme code, and they do not require any additional plugins or libraries to be installed.