WP:发送电子邮件后的标题警告

发送联系表后,我得到以下错误:

Warning: Cannot modify header information - headers already sent by (output started at /home/clientsc/public_html/mypage/wp-includes/general-template.php:2680) in /home/client/public_html/mypage/wp-includes/pluggable.php on line 1171

当我将下面的函数放在名为functions.php的文件中时,我得到了这个错误

function send_my_awesome_form(){

if (!isset($_POST['submit'])) {

    // get the info from the from the form
    $form = array();
    $form['fullname'] = $_POST['fullname'];
    $form['company'] = $_POST['company'];
    $form['email'] = $_POST['email'];
}

// Build the message
$message  = "Name :" . $form['fullname'] ."n";
$message .= "Company :" . $form['company']  ."n";
$message .= "Email :" . $form['email']     ."n";

//set the form headers
$headers = 'From: Contact form <your@contactform.com>';

// The email subject
$subject = 'you got mail';

// Who are we going to send this form too
$send_to = 'myemail@gmail.com';

if (wp_mail( $send_to, $subject, $message, $headers ) ) {
    wp_redirect(home_url( )); exit;
}

 }

 add_action('wp_head', 'send_my_awesome_form');

我该如何解决这个问题?


wp_head挂钩用于主题的标题中,所以太晚了,HTML已经部分显示。

你必须改用init钩子:

add_action('init', 'send_my_awesome_form');

编辑:

function send_my_awesome_form(){ 
    if( isset($_POST['fullname']) && isset($_POST['company']) && isset($_POST['email']) ){ 
        // your form treatment
        // your redirect
    } 
}
链接地址: http://www.djcxy.com/p/69337.html

上一篇: WP: Header warning after sending email

下一篇: Cannot send email in laravel 5.2