1

我正在 Wordpress 中开发我的第一个插件。然后其中一个功能需要发送电子邮件。

但是当我调用函数wp_mail ()时会抛出这个错误:

致命错误:调用未定义的函数 wp_mail ()

我应该如何在我的插件中定义这个函数?

谢谢!

4

3 回答 3

3

在加载 WordPress 环境之前,您可能正在尝试做一些事情。

您的所有操作都应附加到某个钩子上,例如“init”用于前端操作,“admin-init”用于后端,或“wp-ajax-{$your-action-here}”用于 ajax .

检查法典文档:http ://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters

于 2012-12-03T15:11:46.587 回答
0

只需包含 wp-load.php 并继续!

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

或者

require('wp-load.php');

取决于你的脚本是如何加载的。

于 2015-04-29T19:58:44.437 回答
-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.

于 2014-02-10T03:07:19.520 回答