phpwordpresswordpress-plugin-creation

How to deactivate plugin when WooCommerce plugin is not active and show error to user?


Here is my custom plugin code

custom-plugin.php code

include_once FBPE_PLUGIN_PATH . 'inc/class-free-bulk-price-editor.php';
register_activation_hook(__FILE__, array('Free_Bulk_Price_Editor', 'activate'));
register_deactivation_hook(__FILE__, array('Free_Bulk_Price_Editor', 'deactivate'));

class file code:

class Free_Bulk_Price_Editor {
    public static function is_woocommerce_active() {
        return class_exists('WooCommerce');
    }

    public static function activate() {
        if (!self::is_woocommerce_active()) {
            deactivate_plugins('free-bulk-wp-price-upgrade/free-bulk-wp-price-upgrade.php');
            wp_admin_notice( 'There was an error!', [ 'type' => 'error' ] );
            return;
        }
    }
}

This not work according to my requirements I want my plugin only active when WooCommerce is active.

I want to build custom wordpress plugin and want my plugin is active only when WooCommerce is active other wise show error to user that WooCommerce is required to active this plugin.


Solution

  • if you add the line in your plugin code like below

    /*
    Plugin Name:  your pluign name
    Plugin URI:   url
    Description:  A short little description of the plugin.
    Version:      1.0
    Author:       author name
    Requires Plugins: woocommerce
    */