Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ChuckMac/4636464 to your computer and use it in GitHub Desktop.
Save ChuckMac/4636464 to your computer and use it in GitHub Desktop.
WordPress plugin depency check. Check if another plugin is installed before allowing yours to be installed
/**
* Activation Class
**/
if ( ! class_exists( 'WC_CPInstallCheck' ) ) {
class WC_CPInstallCheck {
static function install() {
/**
* Check if WooCommerce & Cubepoints are active
**/
if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ||
!in_array( 'cubepoints/cubepoints.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
// Deactivate the plugin
deactivate_plugins(__FILE__);
// Throw an error in the wordpress admin console
$error_message = __('This plugin requires <a href="http://wordpress.org/extend/plugins/woocommerce/">WooCommerce</a> &amp; <a href="http://wordpress.org/extend/plugins/cubepoints/">Cubepoints</a> plugins to be active!', 'woocommerce');
die($error_message);
}
}
}
}
register_activation_hook( __FILE__, array('WC_CPInstallCheck', 'install') );
@DevinWalker
Copy link

Does this show a message "Plugin could not be activated because it triggered a fatal error." prior to your output?

@kdms90
Copy link

kdms90 commented May 4, 2016

hi,
you can check if another plugin is installed with this method is_plugin_active.
In your example, we have something like this
``
/**

  • Activation Class
    /
    if ( ! class_exists( 'WC_CPInstallCheck' ) ) {
    class WC_CPInstallCheck {
    static function install() {
    /

    * Check if WooCommerce & Cubepoints are active
    **/
    if ( !is_plugin_active('woocommerce/woocommerce.php'))
    // Deactivate the plugin
    deactivate_plugins(FILE);
        // Throw an error in the wordpress admin console
        $error_message = __('This plugin requires <a href="http://wordpress.org/extend/plugins/woocommerce/">WooCommerce</a> &amp; <a href="http://wordpress.org/extend/plugins/cubepoints/">Cubepoints</a> plugins to be active!', 'woocommerce');
        die($error_message);
    }
}

}
}

register_activation_hook( FILE, array('WC_CPInstallCheck', 'install') );
``

@dumketo
Copy link

dumketo commented Jan 26, 2018

it also shows error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment