6

So I have the following in an include in my theme file:

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( is_plugin_active( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php' ) ) {
  $shop_id = woocommerce_get_page_id( 'shop' );
  $shop_page = get_page( $shop_id );
}

but is_plugin_active( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php') is returning false, despite the fact that the plugin is active.

I'm wondering if is_plugin_active() might be tripped up by the theme customizer, because the fact that I'm doing this while hooked to customize_preview_init is the only gotcha that I can imagine would be causing issue. Any insights?

19

is_plugin_active() expects just the base name of the plugin as parameter:

So use:

is_plugin_active( 'woocommerce/woocommerce.php' );

The function will use the option 'active_plugins' which is a list of plugins paths relative to the plugin directory already.

On a multi-site installation it will search in get_site_option( 'active_sitewide_plugins') too.

As an implementation note: Avoid these checks. Some users rename plugin names or directories. Test for the functions you will actually use instead, eg:

if ( function_exists( 'woocommerce_get_page_id' ) )
{
    // do something
}
  • really appreciate that - must have been looking at an outdated commentary on the issue where the full path was included :/ – Brian May 14 '13 at 16:50
2

in case you dont know which plugins are active you can do the following.

// get array of active plugins
$active_plugins = (array) get_option( 'active_plugins', array() );
// see active plugins 'plugin-dir-name/plugin-base-file.php'
echo '<pre>';
print_r( $active_plugins );
echo '</pre>';
if ( ! empty( $active_plugins ) && in_array( 'plugin-dir-name/plugin-base-file.php', $active_plugins ) ) {
    // do something if plugin is active
}

for reference take a look into is_plugin_active function inside 'wp-admin/includes/plugin.php'

function is_plugin_active( $plugin ) {
    return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.