2017-05-31 60 views
1

我在我的插件/文件夹中有一个test.php插件文件,我试图从这个插件发送一封电子邮件。Wp add_action参数错误

我有一个看起来像这样目前是代码我的插件

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

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

勾文档要发送邮件的初始化,您可以编写代码象下面这样:

add_action('init', 'my_custom_init' , 99); 
function my_custom_init() { 
    wp_mail('[email protected]', '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

+0

那么,有没有办法解决这个问题?我能做些什么吗? – meskerem

+0

为什么你要写这个函数?所以我可以建议你适当的胡。你在寻找wp邮件钩子吗? –

+0

是的,我不能在我的插件中使用wp_mail,它说未定义的函数。所以,我需要使用wp_mail发送电子邮件,问题是告诉wp_mail使用主题,身体... – meskerem