admin管理员组文章数量:1291197
I have a plugin which is dependent on another plugin, in this case WooCommerce.
How can I stop the activation of my plugin and display an admin notice with download link of the dependent plugin, if the version of the dependent plugin is lower than a certain version?
In short, on the activation process my plugin should not be activated and display a notice with download link when any of the following is true:
- WooCommerce plugin is not activated (I have solution for this)
- The version of WooCommerce plugin is less than a certain version (I do not have solution for this)
Note: This question is answering my question partially, but I need to have a full solution to check the version of the dependent plugin and display admin notice accordingly.
I have a plugin which is dependent on another plugin, in this case WooCommerce.
How can I stop the activation of my plugin and display an admin notice with download link of the dependent plugin, if the version of the dependent plugin is lower than a certain version?
In short, on the activation process my plugin should not be activated and display a notice with download link when any of the following is true:
- WooCommerce plugin is not activated (I have solution for this)
- The version of WooCommerce plugin is less than a certain version (I do not have solution for this)
Note: This question is answering my question partially, but I need to have a full solution to check the version of the dependent plugin and display admin notice accordingly.
Share Improve this question edited Jun 15, 2021 at 21:29 Tom J Nowell♦ 61k7 gold badges79 silver badges148 bronze badges asked Jun 15, 2021 at 20:50 SiddikaSiddika 134 bronze badges 2- 1 Your plugin must be active for the notice to be displayed, what you are asking for is not possible, but what you want is, if you perform your check in the plugin file then return early if the condition is not met before any of the other code runs, then you can have your admin notice. Similar checks are made in code that tests the version of PHP – Tom J Nowell ♦ Commented Jun 15, 2021 at 21:30
- @TomJNowell thanks for the heads up. The answer I was looking for is here and it worked as I wanted. My plugin does not get activated and yet I get the notice I am looking for. Thanks to Fluent-Themes. – Siddika Commented Jun 16, 2021 at 20:41
1 Answer
Reset to default 0You can add the following code in your plugin. I have used this in my ARB booking plugin and it worked perfectly. The full 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/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();
You will get admin notice like this when your plugin is activated without the Dependent plugin’s desired version:
You see the admin notice has the download link of the dependent plugin and also the user can get back to the plugins page by clicking on the plugins page link.
版权声明:本文标题:How to stop activating a plugin and show admin notice when dependent plugins minimum version is not met 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741500262a2382022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论