admin管理员组文章数量:1122832
I'm developing a Wordpress Woocommerce plugin. On my local environment it works fine but I have problems when adding the plugin to a replica of the prod environment. I am new to wordpress and not very familiar with web dev (I'm a Java programmer).
In the plugin file, I instantiate a class from the Woocommerce plugin package like this:
$coupon = new WC_Coupon($some_code);
In the local environment (php 5.4.10 , Woocommerce 2.0.13, Wordpress 3.6) it's fine. In the production environment (php 5.4.10 , Woocommerce 1.6.5.2, Wordpress 3.4.2) I have the following error:
Fatal error: Class 'WC_Coupon' not found
I have tried including the file where the WC_Coupon class is defined but then the error becomes
Fatal error: Cannot redeclare class WC_Coupon
So what is the proper way to use classes declared in another plugin?
Note: upgrading is not an option at the moment.
I'm developing a Wordpress Woocommerce plugin. On my local environment it works fine but I have problems when adding the plugin to a replica of the prod environment. I am new to wordpress and not very familiar with web dev (I'm a Java programmer).
In the plugin file, I instantiate a class from the Woocommerce plugin package like this:
$coupon = new WC_Coupon($some_code);
In the local environment (php 5.4.10 , Woocommerce 2.0.13, Wordpress 3.6) it's fine. In the production environment (php 5.4.10 , Woocommerce 1.6.5.2, Wordpress 3.4.2) I have the following error:
Fatal error: Class 'WC_Coupon' not found
I have tried including the file where the WC_Coupon class is defined but then the error becomes
Fatal error: Cannot redeclare class WC_Coupon
So what is the proper way to use classes declared in another plugin?
Note: upgrading is not an option at the moment.
Share Improve this question edited Aug 19, 2013 at 13:34 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Aug 19, 2013 at 12:46 znatznat 2372 gold badges3 silver badges7 bronze badges4 Answers
Reset to default 11You have to check if the class exists, but before that you have to wait that all plugin are loaded: no one can assure that your plugin is loaded after WooCommerce.
For run a code from plugin when all plugin are loaded hook into plugins_loaded
hook.
Be aware that you cannot use this hook in a theme, because when theme load that hook was already fired.
add_action('plugins_loaded', 'my_coupon_init');
function my_coupon_init() {
if ( class_exists('WC_Coupon') ) {
$coupon = new WC_Coupon($some_code);
// some code here
} else {
add_action('admin_notices', 'wc_not_loaded');
}
}
function wc_not_loaded() {
printf(
'<div class="error"><p>%s</p></div>',
__('Sorry cannot create coupon because WooCommerce is not loaded')
);
}
This is too late but I would like to share how to use woocommerce and its classes without having an error class not found.
First is to check if woocommerce is installed and use the woocommerce_loaded
action hook.
/**
* Check if WooCommerce is active
**/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
// Put your plugin code here
add_action('woocommerce_loaded' , function (){
//Put your code here that needs any woocommerce class
//You can also Instantiate your main plugin file here
});
}
I hope this helps someone.
The proper way would be :
if( class_exists('WC_Coupon') ) $coupon = new WC_Coupon($some_code);
It's better to check if class exists before using it, it avoids fatal error if the plugin is disabled.
You can't redeclare a class it's not allowed in PHP.
You can also extend class :
class My_WC_Coupon extends WC_Coupon {
//some code
//some hook
}
But most of the time and in this case with WooCommerce you'd better find a hook in documentation that will handle the job.
A "Requires Plugins" header was added in 2024, Introducing Plugin Dependencies in WordPress 6.5
/**
* Plugin Name: Express Payment Gateway Checkout for Shop
* Requires Plugins: shop, payment-gateway
*/
This allows a plugin to depend on another plugin to be activated. Load order is still a concern, and other answers have solutions that wait for all plugins to be loaded and additional check if expected classes exist.
本文标签: oopHow to use classes declared in another plugin
版权声明:本文标题:oop - How to use classes declared in another plugin? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736292444a1928935.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论