skip to Main Content

I’m trying to check if WooCommerce plugin is active then add some options to the option panel. but this code doesn’t work right.

can anyone tell me what is wrong?
thanks.

 if( class_exists( 'WooCommerce' )) {//add options}

2

Answers


  1. Try the following instead:

    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
        // Yes, WooCommerce is enabled
    } else {
        // WooCommerce is NOT enabled!
    }
    
    Login or Signup to reply.
  2. Alternatively, the function is_plugin_active method can be used to check if a plugin is active.

    if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
        // woocommerce is active
    } else {
        // woocommerce is not active
    }
    

    Another method documented in WooCommerce is the below option.

    /**
     * Check if WooCommerce is activated
     */
    if ( ! function_exists( 'is_woocommerce_activated' ) ) {
        function is_woocommerce_activated() {
            if ( class_exists( 'woocommerce' ) ) { return true; } else { return false; }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search