4

I am creating a plugin for WordPress and this plugin depends on another plugin with a specific version. If I enable my plugin without dependent plugin there are fatal error. Is there a way to check this ?

I tried looking at source code but WP do not provide any hook for this.

Thanks in advance.

--UPDATE--

I am not asking about how to check plugin installed or about plugin data, but I want to know "how to check if dependent exists and if no show a warning and don't activate plugin".

6

I was searching for the same answer this morning for my plugin AnsPress. So I sneak into WordPress plugin wp-admin/includes/plugin.php and got an idea.

WordPress check for fatal error while activating plugin, so simplest solution will be trigger a fatal error and this will prevent WordPress to activate the plugin.

In my below code I check if plugin files exists then get plugin version and if lower dependent version trigger error.

function anspress_activate( $network_wide ) {
    //replace this with your dependent plugin
    $category_ext = 'categories-for-anspress/categories-for-anspress.php';

    // replace this with your version
    $version_to_check = '1.3.5'; 

    $category_error = false;

    if(file_exists(WP_PLUGIN_DIR.'/'.$category_ext)){
        $category_ext_data = get_plugin_data( WP_PLUGIN_DIR.'/'.$category_ext);
        $category_error = !version_compare ( $category_ext_data['Version'], $version_to_check, '>=') ? true : false;
    }   

    if ( $category_error ) {
        echo '<h3>'.__('Please update all AnsPress extensions before activating. <a target="_blank" href="http://anspress.io/questions/ask/">Ask for help</a>', 'ap').'</h3>';

        //Adding @ before will prevent XDebug output
        @trigger_error(__('Please update all AnsPress extensions before activating.', 'ap'), E_USER_ERROR);
    }
}

register_activation_hook(__FILE__, 'anspress_activate');

This may not be an elegant solution but it works. feel free to update this answer.

  • Thanks, can I add this to my existing activation hook ? – user1983017 May 23 '15 at 15:52
  • Yes you can, but keep it at top. – Aryan May 23 '15 at 15:56
4

The best way I've come across is based on Ian Dunn plugin. I wrote a plugin for Wordpress which is dependent on Woocommerce and subsequently requires a specific version of woocommerce too. In order to achieve this, I have written the following code. It is important to note that the importance here, which answers your question, is to require_once( ABSPATH . '/wp-admin/includes/plugin.php' ) ; Once you have called this file early, you can verify which plugins are active and the version of such plugins. Here is a short demonstration:

define ( 'WCCF_NAME', 'Woocommerce Plugin Example' ) ;
define ( 'WCCF_REQUIRED_PHP_VERSION', '5.4' ) ;                          // because of get_called_class()
define ( 'WCCF_REQUIRED_WP_VERSION', '4.6' ) ;                          // because of esc_textarea()
define ( 'WCCF_REQUIRED_WC_VERSION', '2.6' );                           // because of Shipping Class system


/**
 * Checks if the system requirements are met
 *
 * @return bool True if system requirements are met, false if not
 */
function wccf_requirements_met () {
    global $wp_version ;
    require_once( ABSPATH . '/wp-admin/includes/plugin.php' ) ;  // to get is_plugin_active() early

    if ( version_compare ( PHP_VERSION, WCCF_REQUIRED_PHP_VERSION, '<' ) ) {
        return false ;
    }

    if ( version_compare ( $wp_version, WCCF_REQUIRED_WP_VERSION, '<' ) ) {
        return false ;
    }

    if ( ! is_plugin_active ( 'woocommerce/woocommerce.php' ) ) {
        return false ;
    }

    $woocommer_data = get_plugin_data(WP_PLUGIN_DIR .'/woocommerce/woocommerce.php', false, false);

    if (version_compare ($woocommer_data['Version'] , WCCF_REQUIRED_WC_VERSION, '<')){
        return false;
    }

    return true ;
}

function wccf_requirements_error () {
    global $wp_version ;

    require_once( plugin_dir_path ( __FILE__ ) . '/admin/partials/requirements-error.php' ) ;
}

if ( wccf_requirements_met() ) {
    require_once( __DIR__ . '/classes/wpps-module.php' );
    require_once( __DIR__ . '/classes/wordpress-plugin-skeleton.php' );
    require_once( __DIR__ . '/includes/admin-notice-helper/admin-notice-helper.php' );
    require_once( __DIR__ . '/classes/wpps-custom-post-type.php' );
    require_once( __DIR__ . '/classes/wpps-cpt-example.php' );
    require_once( __DIR__ . '/classes/wpps-settings.php' );
    require_once( __DIR__ . '/classes/wpps-cron.php' );
    require_once( __DIR__ . '/classes/wpps-instance-class.php' );

    if ( class_exists( 'WordPress_Plugin_Skeleton' ) ) {
        $GLOBALS['wccf'] = WordPress_Plugin_Skeleton::get_instance();
        register_activation_hook(   __FILE__, array( $GLOBALS['wccf'], 'activate' ) );
        register_deactivation_hook( __FILE__, array( $GLOBALS['wccf'], 'deactivate' ) );
    }
} else {
    add_action( 'admin_notices', 'wccf_requirements_error' );
}
2

After testing I found that the currently accepted answer by Aryan, didn't check if the dependency was activated, only if it existed.

This solution will show an error and stop activation if the dependency is missing, and show an error notice if it is later deactivated.

This uses the built in wordpress multisite function

is_plugin_active_for_network($plugin)

but for single site wordpress installs could be substituted for

is_plugin_active($plugin) 

I also post an error notice at the network level if the dependency is later deactivated. Using the hook:

 network_admin_notices

For single site wordpress instead use

admin_notices

Here's the code:

if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
    require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}

 // update to the plugin you are checking for

if ( ! is_plugin_active_for_network( 'acf-network-options-page/acf-network-options-page.php' ) ) {
    function require_acf_network_plugin(){?>
        <div class="notice notice-error" >
            <p> Please Enable ACF Network Options Plugin before using CLN Admin Dashboard</p>
        </div><?php
     @trigger_error(__('Please Enable ACF Network Options Plugin before using CLN Admin Dashboard.', 'cln'), E_USER_ERROR);
    }

    add_action('network_admin_notices','require_acf_network_plugin');
    register_activation_hook(__FILE__, 'require_acf_network_plugin');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.