Support » Plugin: Amount Left for Free Shipping for WooCommerce » function is_plugin_active()

  • Hello,
    I just tried to install the plugin on a multisite installation and for some reason there has been an conflict with Klaviyo for WooCommerce.
    I had a look at the code of both plugins and could fix the problem by replacing your check for active WooCommerce and active Pro-Version by the function is_plugin_active().
    In my opinion the statements used before were absolutely correct, I just wanted to simplify the code and solved the conflict “by mistake” 😉
    Here are the new first lines in left-to-free-shipping-for-woocommerce.php:

    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    // Check if WooCommerce is active
    if ( !is_plugin_active('woocommerce/woocommerce.php') ) {
    	return;
    }
    
    if ( 'left-to-free-shipping-for-woocommerce.php' === basename( __FILE__ ) ) {
    	// Check if Pro is active, if so then return
    	if ( is_plugin_active( 'left-to-free-shipping-for-woocommerce-pro/left-to-free-shipping-for-woocommerce-pro.php' ) ) {
    		return;
    	}
    }
    
    if ( ! class_exists( 'Alg_WC_Left_To_Free_Shipping' ) ) :
    
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Algoritmika Ltd

    (@algoritmika)

    Hi @haet,

    Thanks for the is_plugin_active(), really appreciate it. To be honest, I didn’t know about this function. Will have to change it in all my plugins now :/ Not sure what could cause the compatibility issue with the old code though…

    Plugin Author Algoritmika Ltd

    (@algoritmika)

    Hi again, @haet,

    I’ve tried it out and now I remembered that I actually already tried using is_plugin_active() – it produces Call to undefined function is_plugin_active() error on my server. The problem that this function is only defined in wp-admin/includes/plugin.php file, which loads later. So basically is_plugin_active() should be used in admin_init hook for example. In your case, I assume, some other plugin is requiring wp-admin/includes/plugin.php file earlier, which I’m not sure is a good idea. Anyway, I think I found a solution that should work on all servers – it will be added in next plugin version.

    You could / should wrap your initialization code in

    
    add_action( 'plugins_loaded', function(){
      //  your init code here
    });
    

    so you can make sure the WordPress core is initialized.

    Plugin Author Algoritmika Ltd

    (@algoritmika)

    @haet,

    Yes, I’ve also thought of plugins_loaded hook.

    Thanks for the fast reply!

    Plugin Author Algoritmika Ltd

    (@algoritmika)

    Hi @haet,

    I’ve just tried it with plugins_loaded, but it looks like is_plugin_active() is still not defined yet. The only hook that I found which has this function already defined is admin_init. Also it looks like it’s available in admin only – I’ve tried it in init hook and it’s not defined there.

    So I’m thinking about defining my own function:

    
    /*
     * alg_is_plugin_active.
     */
    function alg_is_plugin_active( $plugin ) {
        return ( function_exists( 'is_plugin_active' ) ? is_plugin_active( $plugin ) :
            (
                in_array( $plugin, apply_filters( 'active_plugins', (array) get_option( 'active_plugins', array() ) ) ) ||
                ( is_multisite() && array_key_exists( $plugin, (array) get_site_option( 'active_sitewide_plugins', array() ) ) )
            )
        );
    }
    

    So basically I’m using is_plugin_active() if it’s already defined (like in your case) and otherwise I’m using my own code (which is basically same code copied from is_plugin_active() source). This should solve the issue on your server. Still not sure why your server doesn’t like my old code though – it’s basically the same code as in original WP.

    Plugin Author Algoritmika Ltd

    (@algoritmika)

    P.S. The only difference I’m seeing in the original WP code and my code is that I’m applying active_plugins filter on get_option( 'active_plugins', array() ). I’ve copied this filter from WooCommerce – in class-wc-helper.php file:

    
    $active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
    
    Plugin Author Algoritmika Ltd

    (@algoritmika)

    P.P.S. I’ve just released new plugin v1.4.1 with the updated code as described above.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.