Wp add_action参数错误

时间:2017-05-31 15:04:47

标签: wordpress

我的插件/文件夹中有一个test.php插件文件,我试图从这个插件发送一封电子邮件。

我的插件中有一个看起来像这样的代码

   add_action('init', 'email_notifier', 10, 5);

   function email_notifier($type, $email, $subject, $body, $link){
    // wp_mail(....)
   }

但是,我不确定导致此错误的原因。

Warning: Missing argument 2 for email_notifier() in C:\....\user\templates_ajax_functions.php on line 35
Warning: Missing argument 3 for email_notifier() in C:\....\user\templates_ajax_functions.php on line 35
Warning: Missing argument 4 for email_notifier() in C:\....\user\templates_ajax_functions.php on line 35
Warning: Missing argument 5 for email_notifier() in C:\....\user\templates_ajax_functions.php on line 35

1 个答案:

答案 0 :(得分:1)

Wordpress init hook没有参数传递,你试图得到它的5个参数。根据您的代码,您似乎使用了错误的钩子。您可以在https://codex.wordpress.org/Plugin_API/Action_Reference/init

上查看init hook文档

对于在init上发送邮件,您可以编写如下代码:

add_action( 'init', 'my_custom_init' , 99 );
function my_custom_init() {
    wp_mail( 'mail@yourmail.com', 'subject', 'body contet of mail' );
}

您可以在https://developer.wordpress.org/reference/functions/wp_mail/

上查看wp_mail功能文档

要更改wp_mail()函数的参数,请参阅以下代码:

add_filter( 'wp_mail', 'my_wp_mail_filter' );
function my_wp_mail_filter( $args ) {

    $new_wp_mail = array(
        'to'          => $args['to'],
        'subject'     => $args['subject'],
        'message'     => $args['message'],
        'headers'     => $args['headers'],
        'attachments' => $args['attachments'],
    );

    return $new_wp_mail;
}

要查看wp_mail过滤器文档,请访问https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_mail

要更改邮件的内容类型,请参阅以下代码:

add_filter( 'wp_mail_content_type', 'set_content_type' );
function set_content_type( $content_type ) {
    return 'text/html';
}

要查看wp_mail_conten_type过滤器的文档,请访问https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_mail_content_type

相关问题