2015-07-04 88 views
0

我正在开发一个woocommerce自定义插件。现在我支持几个woocommerce版本。所以我想检查并显示不兼容性错误,如果有人使用较低版本的woocommerce比我最小的支持。在wordpress插件页面显示插件不兼容错误

我想在我的插件列出的管理面板中的插件页面上显示错误消息。 我有函数来获取woocommerce版本和检查不兼容使用如果条件。但我不知道如何显示错误消息,因为我想要的。

所以请帮助。

在此先感谢。

回答

1

这是我要做的事在我自己的插件:

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挂钩中添加一个功能,以便告诉用户发生了什么。

+0

不知道它是否会产生任何效果,但在误报的情况下(也许您的插件在WC之前加载),您可以尝试将'plugins_loaded'更改为'woocommerce_init'。 – helgatheviking