简体   繁体   English

在Wordpress插件中包含类的正确方法是什么?

[英]What is the proper method for including a class in Wordpress plugin

Forgive me, but I cannot seem to find this seemingly simply answer anywhere. 原谅我,但我似乎无法在任何地方找到这个看似简单的答案。

I have one class (for interacting with MailChimp API) which I am including in my Wordpress plugin. 我有一个类(用于与MailChimp API交互),我将其包含在我的Wordpress插件中。 The problem is, using include inside an action hook isn't working. 问题是,在动作钩子中使用include是行不通的。

For example: 例如:

function load_class() {
    include( plugin_dir_path(__FILE__) . 'includes/MailChimp.class.php');
    }
add_action('plugins_loaded', 'load_class');

doesn't work. 不起作用。

Can someone tell me (with an example) how I should include a php class from within a Wordpress plugin? 有人能告诉我(举个例子)我应该如何在Wordpress插件中包含一个php类?

EDIT: Corrected class name in add_action() function 编辑:更正了add_action()函数中的类名

You said 你说

I have one class (for interacting with MailChimp API) which I am including in my Wordpress plugin. 我有一个类(用于与MailChimp API交互),我将其包含在我的Wordpress插件中。

Why, you are using a hook here ? 为什么,你在这里使用hook You can directly include this class in your working plugin. 您可以直接在您的工作插件中包含此类。 just, include MailChimp.class.php from within your plugin file, as normally you do in php . 只是,从你的插件文件中包含MailChimp.class.php ,就像你在php Also, check the another answer, you have made another mistake and it'is 另外,检查另一个答案,你又犯了一个错误,就是这样

add_action('plugins_loaded', 'load_mailchimp_class');

should be 应该

add_action('plugins_loaded', 'load_class');

but not useful here. 但这里没用。 you don't need to use this. 你不需要使用它。 You asked for an example, include the class from your plugin.php file before you use it, like 你问了一个例子,在你使用它之前包含你的plugin.php文件中的class ,比如

include( plugin_dir_path(__FILE__) . 'includes/MailChimp.class.php');

Your add_action() should read add_action('plugins_loaded', 'load_class'); 你的add_action()应该读取add_action('plugins_loaded', 'load_class'); .

Read more on Codex. 阅读有关Codex的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM