Best way to Abort a Plugin and show Admin Notice when Dependent Plugin’s minimum Version is not met

June 15, 2021
Best way to Abort a Plugin and show Admin Notice when Dependent Plugin’s minimum Version is not met

As a plugin developer you may come to a situation every now and then when your plugin is dependent on another plugin. For example, you may develop an add-on plugin for WooCommerce and your plugin won’t work and may display unknown error messages if your plugin is activated without the WooCommerce plugin.

In addition, your plugin may depend on certain version of the dependent plugin as well. Lets say your plugin will work with WooCommerce version 5.0 and later only. If your plugin is activated when the WooCommerce plugin is not active OR if WooCommerce plugin is active but the version is lower than the required version 5.0, then there is high probability that you will find lots of error messages displayed as soon as your plugin is activated. The situation seems familiar, right? Here I am going to give you the solution so that you will never need to worry about this problem anymore. Thanks to ARB Booking Plugin (an WooCommerce add-on), from where I got this solution.

The following 5 small steps will abort your plugin activation and show admin notice when dependent plugin’s minimum version is not met:

Register the activation hook

/**
* Register the activation hook
*/
public function __construct() {
	register_activation_hook( __FILE__, array( $this, 'ft_install' ) );
}

Check the dependent plugin version

protected $min_wc = '5.0.0'; //replace '5.0.0' with your dependent plugin version number
/**
* Check the dependent plugin version
*/
protected function ft_is_wc_compatible() {			
	return defined( 'WC_VERSION' ) && version_compare( WC_VERSION, $this->min_wc, '>=' );
}

Function to deactivate the plugin

/**
* 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'] );
	}
}

Deactivate the plugin and display a notice if the dependent plugin is not compatible or not active

/**
* Deactivate the plugin and display a notice if the dependent plugin is not compatible or not active.
*/
public function ft_install() {
	if ( ! $this->ft_is_wc_compatible() || ! class_exists( 'WooCommerce' ) ) {
		$this->ft_deactivate_plugin();
		wp_die( 'Could not be activated. ' . $this->get_ft_admin_notices() );
	} else {
		//do your fancy staff here
	}
}

Write the admin notice

/**
* Writing the admin notice
*/
protected function get_ft_admin_notices() {
	return sprintf(
			'%1$s requires WooCommerce version %2$s or higher installed and active. You can download WooCommerce latest version %3$s OR go back to %4$s.',
			'<strong>' . $this->plugin_name . '</strong>',
			$this->min_wc,
			'<strong><a href="https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip">from here</a></strong>',
			'<strong><a href="' . esc_url( admin_url( 'plugins.php' ) ) . '">plugins page</a></strong>'
		);
}

Wrapping it up

The full chunk of the code will be like the following (you can directly copy and paste it in your plugin):

class FT_AdminNotice {
	
	protected $min_wc = '5.0.0'; //replace '5.0.0' with your dependent plugin version number
	
	/**
     * Register the activation hook
     */
	public function __construct() {
		register_activation_hook( __FILE__, array( $this, 'ft_install' ) );
	}
	
	/**
     * Check the dependent plugin version
     */
	protected function ft_is_wc_compatible() {			
		return defined( 'WC_VERSION' ) && version_compare( WC_VERSION, $this->min_wc, '>=' );
	}
	
	/**
     * 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'] );
		}
	}
	
	/**
     * Deactivate the plugin and display a notice if the dependent plugin is not compatible or not active.
     */
	public function ft_install() {
		if ( ! $this->ft_is_wc_compatible() || ! class_exists( 'WooCommerce' ) ) {
			$this->ft_deactivate_plugin();
			wp_die( 'Could not be activated. ' . $this->get_ft_admin_notices() );
		} else {
			//do your fancy staff here
		}
	}
	
	/**
     * Writing the admin notice
     */
	protected function get_ft_admin_notices() {
		return sprintf(
			'%1$s requires WooCommerce version %2$s or higher installed and active. You can download WooCommerce latest version %3$s OR go back to %4$s.',
			'<strong>' . $this->plugin_name . '</strong>',
			$this->min_wc,
			'<strong><a href="https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip">from here</a></strong>',
			'<strong><a href="' . esc_url( admin_url( 'plugins.php' ) ) . '">plugins page</a></strong>'
		);
	}

}

new FT_AdminNotice();

After adding the above code in your plugin, you will get a result/admin notice like the following screen when your plugin is activated without the Dependent plugin’s desired version.

Notice to activate desired version of the dependent plugin

If you need a top notch collection of WordPress themes and plugins, take a look at our Best Collections page.

For more tutorils on WordPress, SEO, Making money online, Google toools etc, just have a look at out at our latest articles

Author
Article by reader87 Author
Published on: June 15, 2021
Last updated on:

Leave a Reply