Check Plugin Dependency on Plugin Activation

If you whant to check a plugin dependency on plugin activation, you can use the snippet below.

The snippet checks, if Timber is installed, if not, you can’t activate the plugin:

class MyPlugin {
    public function __construct() {
        register_activation_hook( __FILE__, array($this, 'plugin_activation_check' ) );
    }

    function plugin_activation_check() {
        if (!is_plugin_active('timber-library/timber.php')) {
             @ deactivate_plugins( plugin_basename( __FILE__ ) );
             wp_die( 'This plugin requires TIMBER to be installed. Please install before activate this plugin.' );
        }
    }
}

new MyPlugin();