custom error message instead of FATAL ERROR in WordPress plugin activation if WooCommerce is not acive

I am trying to give a graceful custom message upon activation of the plugin, if Woocommerce is not activated.

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

The above express is to check whether Woocommerce is active and if it returns false, I do not want the plugin to be activated and wants to throw a custom error. I have tried to stop execution with die() and trigger_error. In these cases, it shows a FATAL ERROR.

Related posts

3 comments

  1. What I usually do is throw and admin notice if WooCommerce isn’t available. And then just stop the running of your plugin.

    if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ){
        add_action( 'admin_notices', 'so_32551934_woocommerce_warning' );
        return; // STOP THE WHOLE PLUGIN
    }
    
    function so_32551934_woocommerce_warning(){ ?>
        <div class="error">
            <p><?php _e( 'Whoa, you're gonna need WooCommerce to run my fancy plugin!', 'my-text-domain' ); ?></p>
        </div>
       <?php 
    }
    
    // The rest of your plugin follows here
    

    Any code that you put here at the end will be run as long as WooCommerce is active. Note, I didn’t test this so be careful of typos.

  2. Have a look at deactivate_plugins function . Try this:

    class MyPlugin {
    
            public function __construct() {
                    register_activation_hook( __FILE__, array( $this , 'activate' ) );
            }
    
            public function activate() {
    
                    // Check if Woo is active
                    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    
                            // Woo is active.
    
                    } else {
    
                            deactivate_plugins( plugin_basename( __FILE__ ) );
                            wp_die( __( 'Please activate WooCommerce.', 'my-plugin' ), 'Plugin dependency check', array( 'back_link' => true ) );
    
                    }
    
                    // Do activate Stuff now.
            }
    }
    
    new MyPlugin();
    
  3. Here my solution:

        class FT_AdminNotice {
    
           /**
           * Register the activation hook
           */
           public function __construct() {
              register_activation_hook( __FILE__, array( $this, 'ft_install' ) );
           }
    
          /**
          * Deactivate the plugin and display a notice if the dependent plugin is not active.
          */
          public function ft_install() {
              if ( ! class_exists( 'WooCommerce' ) ) 
              {
                   $this->ft_deactivate_plugin();
                   wp_die( sprintf(__( 'This plugin requires Woocommerce to be installed and activated.', 'domain-text' ) ) );
              }
          }
    
          /**
          * Function to deactivate the plugin
          */
          protected function ft_deactivate_plugin() {
               require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
               deactivate_plugins( plugin_basename( __FILE__ ) );
               if ( isset( $_GET['activate'] ) ) {
                     unset( $_GET['activate'] );
               }
          }
    
       }
    
       new FT_AdminNotice();
    

    This way show message and you can enrich it like explain here and stop execution too.

Comments are closed.