2017-05-31 61 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