1

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. The problem is, using include inside an action hook isn't working.

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?

EDIT: Corrected class name in add_action() function

2 Answers 2

2

You said

I have one class (for interacting with MailChimp API) which I am including in my Wordpress plugin.

Why, you are using a hook here ? 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. 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

include( plugin_dir_path(__FILE__) . 'includes/MailChimp.class.php');
1
  • Yes, sorry, I had the function correct in my code but mis-typed it here. Thanks for the response! Sep 20, 2013 at 11:51
0

Your add_action() should read add_action('plugins_loaded', 'load_class');.

Read more on Codex.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.