6

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".

4

6 Answers 6

7

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.

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

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' );
}
4

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');
}
0

Here is My simple solution.

add_action('plugins_loaded', function () {

    require_once(ABSPATH . '/wp-admin/includes/plugin.php');

    if (!is_plugin_active('contact-form-7/wp-contact-form-7.php')) {

          deactivate_plugins('my-plugin-folder/my-plugin-file.php');

          add_action('admin_notices', 'aavoya_admin_notice');

    } else {

          require_once 'my-plugin-folder' . '/includes/index.php';
    }

});

/**
 * aavoya_admin_notice
 *
 * @return void
 */
function aavoya_admin_notice()
{
    echo '<div class="error"><p>Plugin deactivated. Please activate contact form 7 plugin!</p></div>';
}
0
<?php
    function check_woocommerce_dependency() {
        // Check if WooCommerce is active
        if (is_plugin_active('woocommerce/woocommerce.php')) {
            // WooCommerce is active
            return;
        }
    
        // WooCommerce is not active, display a notice
        add_action('admin_notices', 'woocommerce_dependency_notice');
    }
    
    function woocommerce_dependency_notice() {
        ?>
        <div class="notice notice-error is-dismissible">
            <p><?php _e('Please activate WooCommerce to use this feature.', 'your-text-domain'); ?></p>
        </div>
        <?php
    }
    
    add_action('admin_init', 'check_woocommerce_dependency');
0

To check for a dependent plugin before activating another plugin, you would typically need to do the following in your code:

  1. Identify the dependent plugin: Determine which plugin your plugin depends on.

  2. Check if the dependent plugin is active: You need to check whether the dependent plugin is currently active or installed.

  3. Activate your plugin conditionally: If the dependent plugin is active, you can proceed to activate your plugin; otherwise, you can skip activation.

Here's a simplified example in pseudo-code:

# Identify the dependent plugin
dependent_plugin_name = "example_dependent_plugin"

# Check if the dependent plugin is active
if is_plugin_active(dependent_plugin_name):
    # Activate your plugin
    activate_plugin("your_plugin_name")
else:
    # Do not activate your plugin
    print("Dependent plugin is not active. Your plugin will not be activated.")

In this code, is_plugin_active() is a function that checks whether a specific plugin is active, and activate_plugin() is a function to activate a plugin. You should adapt this code to the specific programming language and environment you're working with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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