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!
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
Just include wp-load.php and go ahead!
or
Depending how your script are loaded.
The
wp_mail()
function is not available until after the actionplugins_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 afterplugins_loaded
, so if you use that, it should work.