这是我要做的事在我自己的插件:
add_action('plugins_loaded', 'so_31217783_version_test');
function so_31217783_version_test(){
$required_woo = '2.1.0';
if (! defined('WC_VERSION') || version_compare(WC_VERSION, $required_woo, '<')) {
add_action('admin_notices', 'so_31217783_admin_notice');
return false;
}
// add the rest of your actions here
// they will only be triggered if the
// version test has been passed
}
function so_31217783_admin_notice() {
echo '<div class="error"><p>' . sprintf(__('My custom plugins requires at least WooCommerce version %s in order to function. Please upgrade WooCommerce.', 'your-custom-function'), $required_woo) . '</p></div>';
}
基础的解释是,你检查很早就WooCommerce的版本,然后如果最低版本不符合关闭插件。您还可以在admin_notices
挂钩中添加一个功能,以便告诉用户发生了什么。
不知道它是否会产生任何效果,但在误报的情况下(也许您的插件在WC之前加载),您可以尝试将'plugins_loaded'更改为'woocommerce_init'。 – helgatheviking