1

I'm developing my first plugin in Wordpress. Then one of the functions need to send an email.

But when I call the function wp_mail () throws me this error:

Fatal error: Call to undefined function wp_mail ()

How should I define this function in my plugin??

Thanks!

3 Answers 3

3

You're probably trying to do something before loading the WordPress enviroment.

All of your actions should be attached to some hook, for instance "init" for actions on the front-end, "admin-init" on the backend, or "wp-ajax-{$your-action-here}" for ajax.

Check the codex docs: http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters

0
0

Just include wp-load.php and go ahead!

require('../../../wp-load.php');

or

require('wp-load.php');

Depending how your script are loaded.

-1

The wp_mail() function is not available until after the action plugins_loaded, so if you try to use it before that point it will throw an error because the function is undefined at that point.

So rather than simply calling wp_mail(), you should be hooking that process to an action so that it occurs after the function has been loaded.

A lot of plugins use init as the action hook to connect their action to. init comes after plugins_loaded, so if you use that, it should work.

2
  • wp_mail() is not a plugin , it is Pluggable Function ! situated in "wp-includes\pluggable.php " , and it will not work , till wp_config.php it will be loaded !
    – Hichem
    Mar 11, 2015 at 13:36
  • While that is a correct statement, calling wp_config.php in your plugin is not the proper way of making sure that wp_mail is available for use. The correct way is to make sure that whatever process you are using wp_mail in happens after the plugins_loaded action, at which time wp_mail would be loaded by either another plugin OR by inclusion from pluggable.php.
    – butlerblog
    Mar 11, 2015 at 15:30

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.