您可以在插件中添加以下代码。我已经在我的ARB预订插件中使用了它,它工作得非常好。完整的details of the code with explanation can be found here:
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();
当您的插件在没有依赖插件所需版本的情况下被激活时,您将收到如下管理员通知:
您可以看到管理员通知中有依赖插件的下载链接,用户也可以通过单击插件页面链接返回插件页面。